添加 dom 元素的 jquery 实时事件

发布于 2024-09-25 17:04:01 字数 389 浏览 0 评论 0原文

我想向页面上现在或将来具有指定类并满足某些条件的任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-10-02 17:04:01

jQuery 的 live() 功能只是 livequery 插件的子集,后者要丰富得多。如果您使用 livequery,您可以执行类似的操作...

$('.location').livequery(function() {
   // perform selector on $(this) to apply class   
});

这将覆盖现有元素以及添加到 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..

$('.location').livequery(function() {
   // perform selector on $(this) to apply class   
});

That will cover existing elements plus any future elements added to the DOM.

吝吻 2024-10-02 17:04:01

你不能这样做。

您必须执行以下操作:

$('.location').filter(function () {
    return ($(this).find('input:first').val().substr(0, 1) == "!");
}).addClass('hidden');

将其应用于页面上当前的所有元素,然后手动将“隐藏”类添加到您添加到 DOM 的未来元素。

You can't do this.

You'll have to do:

$('.location').filter(function () {
    return ($(this).find('input:first').val().substr(0, 1) == "!");
}).addClass('hidden');

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.

起风了 2024-10-02 17:04:01

代码 .subtr(0,1) = "!" 可能不会执行您想要的操作。

The code .subtr(0,1) = "!" likely doesn't do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文