gpt4 book ai didi

jQuery:如何用 .on 替换 .live?

转载 作者:行者123 更新时间:2023-12-03 21:27:01 25 4
gpt4 key购买 nike

Possible Duplicate:
jQuery 1.7 - Turning live() into on()

根据 jQuery API ( http://api.jquery.com/on/ ),函数 'live' 已被弃用,建议使用 'on' 代替。但是当我在代码中将“live”替换为“on”时,jQuery 无法再找到后来添加的元素:

这有效(但已弃用):

$('li.bibeintrag').live('click', function(){
alert('myattribute =' + $(this).attr('myattribute'));
});

这是来自“on”API 的示例:

$("#dataTable tbody tr").on("click", function(event){
alert($(this).text());
});

当我将代码更改为此(“live”替换为“on”)时,它不再起作用(jQuery 将找不到后来添加的元素(例如使用附加)):

$('li.bibeintrag').on('click', function(){
alert('myattribute =' + $(this).attr('myattribute'));
});

我做错了什么?有人可以帮忙吗?

最佳答案

您应该将事件添加到 li.bibeintrag 的任何父元素。

$('ul').on('click', "li.bibeintrag", function(){
alert('myattribute =' + $(this).attr('myattribute'));
});

关于jQuery:如何用 .on 替换 .live?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604328/

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