添加dom元素的jquery live event

时间:2023-01-03 00:04:08

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

我想为现在或将来具有指定类并符合某些条件的页面上的任何DOM元素添加一个类

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');
});

3 个解决方案

#1


24  

jQuery's live() feature is just subset of the livequery plugin, which is much richer. If you use livequery you could do something like..

jQuery的live()功能只是livequery插件的子集,它更加丰富。如果你使用livequery,你可以做类似的事情。

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

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

这将涵盖现有元素以及添加到DOM的任何未来元素。

#2


0  

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.

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

#3


0  

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

代码.subtr(0,1)=“!”可能不会做你想要的。

#1


24  

jQuery's live() feature is just subset of the livequery plugin, which is much richer. If you use livequery you could do something like..

jQuery的live()功能只是livequery插件的子集,它更加丰富。如果你使用livequery,你可以做类似的事情。

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

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

这将涵盖现有元素以及添加到DOM的任何未来元素。

#2


0  

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.

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

#3


0  

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

代码.subtr(0,1)=“!”可能不会做你想要的。