简体   繁体   English

jQuery:与插件一起使用

[英]Jquery: Using live with plugins

Having a table with class .data, I want to apply setMask, usual way is: 有一个带有类.data的表,我想应用setMask,通常的方法是:

$(".data tbody tr input:text").setMask();

However I dynamically add new rows, thus a live function is needed for future created ones. 但是我动态添加新行,因此将来创建的行需要一个实时函数。 I have tried this but failed: 我已经尝试过但是失败了:

$('.data tbody tr input:text').live('ready', function() {
    $(this).setMask();
});

I would like to find a solution to this. 我想找到一个解决方案。

Use liveQuery plugin. 使用liveQuery插件。 You can just bind with out any events. 您可以绑定任何事件。

$('.data tbody tr input:text').livequery(function() {
    $(this).setMask();
});

You can only bind existing events to live and bind . 您只能将现有事件绑定到livebind For the life of me, I can't find a complete list (have tried many times), but ready and load are not on there: that is, there is no way to bind an element being loaded, at least not across browsers. 在我的一生中,我找不到完整的列表(已经尝试了很多次),但是没有ready load的内容:也就是说,无法绑定正在加载的元素,至少没有跨浏览器的方式。 If you only care about non-IE, then I believe DomNodeInserted will work as an event. 如果您只关心非IE,那么我相信DomNodeInserted将作为一个事件工作。

Either way, you seem to control when you are inserting the table row, no? 无论哪种方式,您似乎都可以控制何时插入表格行,不是吗? So when you insert the table row, then just run setMask on it, in this vein: 因此,当您插入表格行时,只需按照以下方式对其运行setMask:

$tr = $(".data tbody").insertTableRow(); //Pretend function returns tr object
$tr.setMask();

Take a look at livequery plugin. 看一下livequery插件。 It's a little different form $.live in that it can fire a function when a new matched element is added. 它与$ .live形式略有不同,因为它可以在添加新的匹配元素时触发一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM