gpt4 book ai didi

jquery - 将事件处理程序附加到所有具有类刷新的 span 元素的直接父级

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:24 24 4
gpt4 key购买 nike

我再次来到这里寻求帮助,非常感谢您的建议。

我有一个小元素要处理。那么我需要的是使用 jQuery 查找所有具有类刷新的 span 元素,并将点击处理程序附加到其直接父级。我可以使用 jQuery 的 bind(),但正如我们所知,它不适用于将来添加的元素。如果我在过去的黄金时代,我会使用 live() 但由于它已被弃用,我不能使用它。

我的另一个选择是使用委托(delegate)函数,但使用委托(delegate)函数我无法传递选择器参数。

这是我的 fiddle 。 http://jsfiddle.net/ardeezstyle/hRhT7/2/

$('.refresh').parent().bind('click',function(){
$(this).css({'position':'relative','background':'#fff'}).append('<span>').find('span:last').addClass('refreshing');

_this=$(this);
setTimeout(function(){
_this.removeAttr('style').find('span.refreshing').remove();
}, 1000);

});

如有任何帮助,我将不胜感激。

干杯!

最佳答案

在委托(delegate)事件上使用

var parentID=$('.refresh').parent().attr('id'); // using id of parent.. you can get any attributes of parent here...if id is not present..
$(document).on('click','#'+parentID,function(){
$(this).css({'position':'relative','background':'#fff'}).append('<span>').find('span:last').addClass('refreshing');

_this=$(this);
setTimeout(function(){
_this.removeAttr('style').find('span.refreshing').remove();
}, 1000);

});

最好使用文档中最接近的静态父容器,然后使用文档本身以获得更好的性能。link阅读有关事件的更多信息

关于jquery - 将事件处理程序附加到所有具有类刷新的 span 元素的直接父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16294002/

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