jQuery:我可以自动将插件应用于动态添加的元素吗? [英] jQuery: Can I automatically apply a plug-in to a dynamically added element?
问题描述
我正在将Web应用程序转换为完全AJAX架构.
I'm in the process of converting my web app to a fully AJAX architecture.
我有一个初始加载的母版页和一个加载了动态内容的div容器.
I have my master page that is initially loaded and a div container that is loaded with dynamic content.
我创建了一些适用于某些元素的jQuery插件,以扩展其功能.我通常会在每次页面加载期间按如下方式调用这些函数:
I created a few jQuery plugins that I apply to certain elements in order to extend their functionality. I'd normally call the functions as follows during each page load:
$(document).ready(function () {
// Enable fancy AJAX search
$(".entity-search-table").EntitySearch();
});
这将找到合适的div,然后调用插件以启用必要的功能.
This would find the appropriate div(s) and call the plugin to enable the necessary functionality.
在AJAX环境中,我不能仅在页面加载期间应用插件,因为元素会动态添加和删除.
In an AJAX environment I can't just apply the plugin during the page load since elements will be added and removed dynamically.
我想做这样的事情:
$(document).ready(function () {
// Enable fancy AJAX search
$(".entity-search-table").live("load", function () {
$(this).EntitySearch();
});
});
问题:将<div>
或其他与选择器匹配的元素添加到DOM时,我有什么方法可以触发事件?
Question: Is there any way that I can trigger an event when a <div>
or other element that matches a selector is added to the DOM?
每次AJAX请求完成时激活插件似乎非常浪费.第一次将插件添加到DOM时,只需将其应用于该元素.
It seems incredibly wasteful to activate the plug-in every time an AJAX request completes. The plug-in only needs to be applied to the element once when it is first added to the DOM.
感谢您的帮助!
推荐答案
是-看一下 liveQuery .示例:
$('.entity-search-table').livequery(function(){
$(this).EntitySearch();
});
这篇关于jQuery:我可以自动将插件应用于动态添加的元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!