为什么这不起作用?
$(document).ready(function(){
var content = '<a href="http://example.com/index.php><b>Some text!</b></a> - <a href="http://example.com/index.php" class="ot-origin-anchor">News</a>';
$("#test").replaceWith(function(){
return content;
});
//Here is the problem. I don't know why but I can't define adres.
var adres = $("#test .ot-origin-anchor").attr("href");
//find example.com - ugly :P
var adresRegExp = adres.match(/(\w+:\/\/+(www.|))([^/]+)/);
alert(RegExp.$3);
});
</script>
<div id="test">bnb</div>
最佳答案
在.replaceWith()
之后调用时,页面上没有 ID 为 test
的元素。看来您打算使用 .html()
或.append()
而不是 .replaceWith()
。
var content = '<a href="http://example.com/index.php><b>Some text!</b></a> - <a href="http://example.com/index.php" class="ot-origin-anchor">News</a>';
$("#test").html(content);
// or
$("#test").append(content);
var adres = $("#test .ot-origin-anchor").attr("href");
关于javascript - 为什么 jQuery 选择器不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7151789/