使用jQuery live()初始化插件? [英] Using jQuery live() to initialize plugins?
问题描述
使用jQuery,在特定类的所有当前和未来元素上自动初始化插件的最佳方法是什么?
Using jQuery, what's the best way to automatically initialize a plugin on all current and future elements of a specific class?
例如,假设我想要所有< input type =textclass =datepicker/>
要素 jQuery UI Datepicker 插件,包括我可能在运行时创建的任何插件。
For example, say I want all <input type="text" class="datepicker" />
elements to have the jQuery UI Datepicker plugin, including any I might create at runtime.
基本上,我想做类似这样的事情:
Essentially, I want to do something like this:
$('.datepicker').live('create', function() {
$(this).datepicker();
});
但是,当然,我没有创建事件使用。
But, of course, there isn't a create event I can use.
推荐答案
您可以使用 .livequery()
插件,报告因而导致死亡.live()
被夸大了:)
You can use the .livequery()
plugin for this, reports of it's death due to .live()
have been greatly exaggerated :)
.live()
监听事件泡沫所以它的用途略有不同。使用 .livequery()
你可以达到你想要的效果:
.live()
listens for event to bubble so it serves a slightly different purpose. With .livequery()
you'd achieve what you want like this:
$('.datepicker').livequery(function() {
$(this).datepicker();
});
这将在当前和未来 上运行。 datepicker
元素。
这篇关于使用jQuery live()初始化插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!