Javascript 如何使用参数调用live方法
我正在学习如何编写jQuery插件。所以我为什么要做我正在做的事并不重要 我正在编写一个名为live2的插件,它除了在内部调用livemethod之外什么都不做Javascript 如何使用参数调用live方法,javascript,jquery,Javascript,Jquery,我正在学习如何编写jQuery插件。所以我为什么要做我正在做的事并不重要 我正在编写一个名为live2的插件,它除了在内部调用livemethod之外什么都不做 (function($) { $.fn.live2 = function() { /* if there are no elements then just return */ if (!this.length) return this; return this.each(f
(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代替live
$('#container').live2('click',function(){
return false;
})
但是插件不起作用。任何关于修复的想法。编辑:我认为您只需要执行以下操作:
$.fn.live2 = function() {
this.live.apply(this, arguments);
};
live仅适用于选择器,因此
$('something.something').live(…)
可以
$(此).live(…
)不应该工作(根据文档)
您的插件示例看起来很不错。我要改变这两件事:
1.这个换成这个美元
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);
您是否在该行中缺少一美元:
jQuery.fn.live.apply(this, arguments);
所以,
jQuery.fn.live.apply($this, arguments);
你想完成什么?您只需:
jQuery.fn.live2=jQuery.fn.live代码>这是描述我在编写更大的插件时所面临问题的最小级别。在某些情况下,我需要调用live方法,在其他情况下,我需要做一些事情。我被困在我需要的地方,只需要激活live并离开。