我尝试在 #foo anchor 标记之后为新添加的 anchor 标记 (.he) 附加单击事件。但点击事件没有发生。我用过(.on)。你们能帮我吗?*
<!DOCTYPE html>
<html>
<head>
<title>Attaching event</title>
<style>p { background:yellow; margin:6px 0; }</style>
<script src="js/jquery.js"></script>
</head>
<body>
<p>Hello</p>
how are
<p>you?</p>
<button>Call remove() on paragraphs</button>
<a id="foo" href="#" style="display:block">Testing the bind and unbind</a>
<script>
$("button").on('click',function () {
$("#foo").after("<a class='he' href='#'>asdfasdf</a>");
});
$(".he").on('click', function(){
alert("test");
});
</script>
</body>
最佳答案
您必须委托(delegate)给这样的静态父元素
$(document).on('click','.he',function(){
//code here
});
关于javascript - 如何使用 jquery 将事件附加到新添加的 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14975669/