gpt4 book ai didi

jquery - 实时处理不适用于 event.stopPropagation();

转载 作者:行者123 更新时间:2023-12-01 04:14:03 24 4
gpt4 key购买 nike

我正在使用 event.stopPropagation(),但在父级上使用 .live() 处理程序时遇到问题。

//does not work
$("#testlink").live({
click: function(event)
{
alert('testlink');
}
});

//works great!
$("#testlink").click(function() {
alert('testlink');
});

我必须使用 .live() 因为内容是通过 AJAX 加载的。

有人可以帮我解决这个问题吗?

<小时/>

我怎样才能使用更多这样的类或ID?

$('#div1', '#div2', '#div3').on('click', 'a.testlink', function(){
alert('testlink');
});

这怎么可能?

最佳答案

使用on() (正如 @techfoobar 提到的, live() 在版本 1.7 中已弃用,并从版本 1.9 起删除)和事件委托(delegate)(如果内容是动态生成的):

$('#container').on('click', '#testlink', function(){
alert('testlink');
});

其中 #container 是包含动态加载的 #testLink 元素的元素(您可以使用 document,但此处使用容器元素会减少搜索范围并提高效率)。

关于jquery - 实时处理不适用于 event.stopPropagation();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16627308/

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