我试图在#foo锚标记之后为新添加的锚标记(.he)附加一个click事件.但点击事件没有发生.我用过(.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>
Run Code Online (Sandbox Code Playgroud)
您必须委托给像这样的静态父元素
$(document).on('click','.he',function(){
//code here
});
Run Code Online (Sandbox Code Playgroud)