jQuery:我可以自动将插件应用到动态添加的元素吗? [英] jQuery: Can I automatically apply a plug-in to a dynamically added element?
问题描述
我正在将我的网络应用程序转换为完全 AJAX 架构.
我有一个初始加载的母版页和一个加载了动态内容的 div 容器.
我创建了一些 jQuery 插件,我将它们应用于某些元素以扩展它们的功能.在每个页面加载期间,我通常会按如下方式调用函数:
$(document).ready(function () {//启用花哨的 AJAX 搜索$(".entity-search-table").EntitySearch();});
这将找到合适的 div(s) 并调用插件以启用必要的功能.
在 AJAX 环境中,我不能只在页面加载期间应用插件,因为元素将被动态添加和删除.
我想做这样的事情:
$(document).ready(function () {//启用花哨的 AJAX 搜索$(".entity-search-table").live("load", function () {$(this).EntitySearch();});});
问题:当 <div>
或其他匹配选择器的元素添加到 DOM 时,我有什么方法可以触发事件?p>
每次 AJAX 请求完成时激活插件似乎非常浪费.插件只需要在元素首次添加到 DOM 时应用一次即可.
感谢您的帮助!
是的 - 看看 liveQuery.示例:
$('.entity-search-table').livequery(function(){$(this).EntitySearch();});
I'm in the process of converting my web app to a fully AJAX architecture.
I have my master page that is initially loaded and a div container that is loaded with dynamic content.
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();
});
This would find the appropriate div(s) and call the plugin to enable the necessary functionality.
In an AJAX environment I can't just apply the plugin during the page load since elements will be added and removed dynamically.
I'd like to do something like this:
$(document).ready(function () {
// Enable fancy AJAX search
$(".entity-search-table").live("load", function () {
$(this).EntitySearch();
});
});
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?
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.
Thanks for any help!
Yes - take a look at liveQuery. Example:
$('.entity-search-table').livequery(function(){
$(this).EntitySearch();
});
这篇关于jQuery:我可以自动将插件应用到动态添加的元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!