用于添加 dom 元素的 jquery 实时事件 [英] jquery live event for added dom elements

查看:29
本文介绍了用于添加 dom 元素的 jquery 实时事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以对于一些伪代码

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

当然这没什么

编辑注释

这也不起作用

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

推荐答案

jQuery 的 live() 功能只是 livequery 插件的一个子集,它更加丰富.如果您使用 livequery,您可以执行以下操作...

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

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

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

这篇关于用于添加 dom 元素的 jquery 实时事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆