gpt4 book ai didi

jquery append 和 on 方法

转载 作者:行者123 更新时间:2023-11-28 05:00:24 25 4
gpt4 key购买 nike

我有这个页面:

<html>
<head></head>
<body>

<div id="elem"></div>
<button id="addElem">Add Element</button>

<script src="jquery-1.9.1.js" type="text/javascript"></script>

<script type="text/javascript">
$(function () {

//add element to elem div
$("#addElem").click(function () {
$("#elem").append('<a id="test" href="#">this is a test</a><br />');
});

//remove added elements on click event
$("#test").on("click", function(){
alert(12);
});

});
</script>

</body>
</html>

它正在做的是添加一个链接,我希望能够单击添加的链接并显示一条警告消息,但该警告不起作用。我错过了什么?

最佳答案

将其更改为...

$("#elem").on("click", "#test", function(){
alert(12);
});

jQuery blog对此有很好的解释。

$(elements).on( events [, selector] [, data] , handler );

When a selector is provided, .on() is similar to .delegate() in that it attaches a >delegated event handler, filtered by the selector. When the selector is omitted or null >the call is like .bind(). There is one ambiguous case: If the data argument is a string, >you must provide either a selector string or null so that the data isn’t mistaken as a >selector. Pass an object for data and you’ll never have to worry about special cases.

关于jquery append 和 on 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326339/

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