我正在学习如何编写jQuery插件。所以我为什么要做我正在做的事情并不重要。
我编写了一个名为live2的插件,它除了在内部调用live方法之外,什么也不做。
(function($) {
$.fn.live2 = function() {
/* if there are no elements then just return */
if (!this.length) return this;
return this.each(function() {
var $this = $(this);
jQuery.fn.live.apply(this, arguments);
}); // end of return this.each(function())
}; // end of plugin
})(jQuery);上面的代码应该在任何活动的方法中调用。而不是实时使用live2。
$('#container').live2('click',function(){
return false;
})但是插件不起作用。你知道该怎么解决吗?
发布于 2010-02-19 06:29:29
live仅适用于选择器,因此
$('#something.something').live(...)将起作用
$(this).live(...)不能工作(根据文档)
您的插件示例看起来还不错。我会改变这两件事: 1.这变成了$this 2.在调用每个函数后返回
live并不是一个很好的实验函数。尝试切换
如下所示:
(function($) {
$.fn.toggle2 = function() {
/* if there are no elements then just return */
if (!this.length) return this;
this.each(function() {
var $this = $(this);
$this.toggle(arguments);
});
return this;
}; // end of plugin
})(jQuery);发布于 2010-02-19 03:10:26
编辑:我认为你只需要这样做:
$.fn.live2 = function() {
this.live.apply(this, arguments);
};发布于 2010-04-10 13:13:04
您是否在这一行中遗漏了一个$:
jQuery.fn.live.apply(this, arguments);因此,
jQuery.fn.live.apply($this, arguments);https://stackoverflow.com/questions/2291360
复制相似问题