我知道您可以使用附加事件
$(selector).on("click", "#myelement", function()
{ ...
}).
但我想做这样的事情:
$("body").on("tagsinput", "#myelement", function()
{ ...
}).
我希望我的动态元素“#myelement”在 DOM 中时使用 Tagsinput 插件
主要是当元素已经在 DOM 中时我使用了 jquery 插件,如下所示:
$(function (){ $("#myelement").tagsinput(); });
救命救命! :D
最佳答案
当用户单击元素时,您可以初始化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();
}
});
关于javascript - 使用 Jquery On 将动态事件附加到元素(在动态元素上使用 jquery 插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29833541/