[英]Attach dynamic events to elements with Jquery On (using jquery plugins on dynamic elements)
I know you can attach events using 我知道您可以使用
$(selector).on("click", "#myelement", function()
{ ...
}).
but I want to do something like this: 但我想做这样的事情:
$("body").on("tagsinput", "#myelement", function()
{ ...
}).
I want my dynamic element "#myelement" to use tagsinput plugin when it is in the DOM 我希望动态元素“ #myelement”在DOM中时使用tagsinput插件
mainly I used jquery plugins when the element is already in the DOM like this: 主要是当元素已经在DOM中时使用jquery插件,如下所示:
$(function (){ $("#myelement").tagsinput(); });
Help help! 帮帮忙! :D
:d
You could initialise tagsinput (if it exists) when the user clicks on the element, which I think will solve your problem. 您可以在用户单击元素时初始化tagsinput(如果存在),我认为这可以解决您的问题。
$(document).on("click", "#myelement", function() {
// don't add the functionality twice
if ($(this).data("tagsinput-initialised")) return;
// don't try to add functionality if plugin hasn't loaded
if ($.isFunction($.fn.tagsInput)) {
// "save" that we've applied the functionality
$(this).data("tagsinput-initialised", true);
// do it
$(this).tagsinput();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.