开发者

How invoke live method with arguments

I am learning how to write jQuery plugin. So why I am doing what I am doing does not matter.

I writing a plugin called live2 which does nothing else but internally calls live method.

(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);

Above code should be invoked just live any live method. Instead of live use live2.

$('#container').live2('click',function(){
return false;
})

But 开发者_StackOverflow中文版the plugin is not working. Any idea what the fix should be.


live only works with a selector, so

$('#something.something').live(...) will work

$(this).live(...) is not supposed to work (according to documentation)

Your plugin example looks quite ok. Id change this two things: 1. this into $this 2. return after calling each

And live is not a good function to do the experiments with. Try toggle

like this:

(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);


EDIT: I think you just need to do this:

$.fn.live2 = function() {
    this.live.apply(this, arguments);
};


Are you missing a $ in this line:

  jQuery.fn.live.apply(this, arguments);

Therefore,

  jQuery.fn.live.apply($this, arguments);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜