【发布时间】:2010-12-23 14:04:55
【问题描述】:
我有很多关于编写面向对象的javascript代码和开发jQuery插件的文章,到目前为止都很好,我了解它们是如何工作的,我可以创建自己的插件。
但是,所有文章都存在一个问题(即使有官方插件创作指南 - http://docs.jquery.com/Plugins/Authoring) - 这些所有模式都不支持“实时”。
让我们以这种模式为例 - http://www.virgentech.com/blog/2009/10/building-object-oriented-jquery-plugin.html
$.fn.myplugin = function(options)
{
return this.each(function()
{
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('myplugin')) return;
// pass options to plugin constructor
var myplugin = new MyPlugin(this, options);
// Store plugin object in this element's data
element.data('myplugin', myplugin);
});
};
将在每个 jQuery 匹配对象上创建新的“MyPlugin”实例。
如何更改它(如果可能的话),以便它适用于将来添加的元素?
谢谢
【问题讨论】:
-
这些元素是如何添加的?
-
它们可以使用 ajax 添加,我知道可以使用回调重新应用插件,但这似乎是一种浪费,因为将创建新对象
标签: jquery jquery-plugins live plugin-pattern