gpt4 book ai didi

javascript - 如何在 jquery 上的脚本中将 "live"更改为 "on"以获得相同的功能

转载 作者:行者123 更新时间:2023-12-02 16:58:41 25 4
gpt4 key购买 nike

我想将 jQuery 代码从 live() 函数转换为具有相同功能的 on() 函数。

如果我单击一个段落创建另一个段落,当我单击跨越“删除”时,该段落将被删除。

如果我将 live() 更改为 on(),如果我单击第一个段落而不是她之前创建的每个段落,则会创建该段落。

演示代码在这里:http://jsfiddle.net/ny38cLba/6/

HTML 代码

<body>
<p>Paragraph 1</p>

</body>

JS代码

$(document).ready(function() {
var i = 2;
$("p").live("click", function() {
$("<p>Paragraph " + i++ +" <span> Delete</span></p>").insertAfter(this);
});
$("span").live("click", function() {
$(this).parent().remove();
});
});

如果我想顺利隐藏一个段落,我使用以下代码:

$(this).on("click", "span",  function(){
$(this).parent().hide("slow",function(){
$(this).remove();
});
})

但是删除该段落后,会出现而不是新的段落。

请参阅此代码:http://jsfiddle.net/a8e9v2m5/2/

最佳答案

这在文档中:http://api.jquery.com/live/

$( document ).on( events, selector, data, handler ); // jQuery 1.7+

所以,

$(document).on("click", "p", function() {
$("<p>Paragraph " + i+++" <span> Delete</span></p>").insertAfter(this);
});
$(document).on("click", "span", function() {
$(this).parent().remove();
});

关于javascript - 如何在 jquery 上的脚本中将 "live"更改为 "on"以获得相同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25957765/

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