开发者

jquery live event for added dom elements

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')}
});
开发者_StackOverflow中文版

of course this does nothing

EDIT NOTE

this does not work either

$('.location').live('load',function(){
    alert('adding location');
});


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜