添加 dom 元素的 jquery 实时事件
我想向页面上现在或将来具有指定类并满足某些条件的任何 DOM 元素添加一个类,
因此对于某些伪代码
$('.location').live('load',function(){
if($(this).find('input:first').val().substr(0,1) == "!"){ $(this).addClass('hidden')}
});
来说,这当然没有任何作用
编辑注意
这确实也不工作
$('.location').live('load',function(){
alert('adding location');
});
I want to add a class to any DOM element that is on the page now or in the future that has a specified class and meets some criteria
so for some pseudo code
$('.location').live('load',function(){
if($(this).find('input:first').val().substr(0,1) == "!"){ $(this).addClass('hidden')}
});
of course this does nothing
EDIT NOTE
this does not work either
$('.location').live('load',function(){
alert('adding location');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 的 live() 功能只是 livequery 插件的子集,后者要丰富得多。如果您使用 livequery,您可以执行类似的操作...
这将覆盖现有元素以及添加到 DOM 的任何未来元素。
jQuery's live() feature is just subset of the livequery plugin, which is much richer. If you use livequery you could do something like..
That will cover existing elements plus any future elements added to the DOM.
你不能这样做。
您必须执行以下操作:
将其应用于页面上当前的所有元素,然后手动将“隐藏”类添加到您添加到 DOM 的未来元素。
You can't do this.
You'll have to do:
To apply it to all currently elements on the page, and then manually add the 'hidden' class to future elements you add to the DOM.
代码
.subtr(0,1) = "!"
可能不会执行您想要的操作。The code
.subtr(0,1) = "!"
likely doesn't do what you want.