简体   繁体   English

如何将jquery插件应用于新创建的元素

[英]How do I apply jquery plugin to newly created element

I have a third party function that I use. 我有一个我使用的第三方功能。 To use ii I just run: 要使用ii,我只需运行:

$('.myElements').thirdPartyFunction(options);

It works fine. 它工作正常。 However if I dynamically add an element with class .myElements , how can I have it run the thirdPartyFunction on that that element? 但是,如果我动态添加类.myElements的元素,我怎么能让它在该元素上运行thirdPartyFunction?

If you're not worried about performance you can use DOMNodeInserted 如果您不担心性能,可以使用DOMNodeInserted

$(document).bind("DOMNodeInserted"), function(event){
      $(event.target).find(".myElements").addBack(".myElements").thirdPartyFunction();
}

Though it is not very performant, so you may wish to look at something else. 虽然它不是很高效,但您可能希望看一下其他内容。

Other options: Alternative to DOMNodeInserted 其他选项: DOMNodeInserted的替代方案

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

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