gpt4 book ai didi

javascript - 追加后jquery 1.7点击事件不起作用

转载 作者:行者123 更新时间:2023-11-30 07:08:54 24 4
gpt4 key购买 nike

我正在研究来自 http://learn.jquery.com/events/event-delegation/ 的事件委托(delegate).我有以下代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="A small web app to manage todos">
<title>TO-DO jQuery Tests</title>

<!--src attribute in the <script> element must point to a copy of jQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$( "article" ).on( "click","a", function() {
console.log( "The ID attribute of the link is: " + $(this).attr('id'));
});
//The .append()method inserts the specified content as the last child of each element in the jQuery collection -->
$( "#test-container" ).append("<article> <a href=\"#\"id='link-2'>Link 2</a> </article>" );
});
</script>
</head>
<body>
<div id="test-container">
<article>
<a href="#" id="link-1">Link 1</a>
</article>
</div>
</body>

当我点击 Link2 时,控制台上没有输出?为什么事件处理不起作用?

最佳答案

你必须绑定(bind)到最近的静态父级。
本例 test-container.

$( "#test-container" ).on( "click","article a", function() {
// your code
});

关于javascript - 追加后jquery 1.7点击事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20640620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com