0

我在 jQuery 中创建了自己的方法:

$.fn.plop = function(){
    // Method 
};

我试图在队列中调用它,例如:

$(elems[0]).plop().delay(800).fadeOut();

但是 Firefox 返回错误:

类型错误:$(...).plop(...) 未定义

关于导致问题的任何想法?谢谢。

4

2 回答 2

1

如果你希望它是可链接的,你需要在你的函数中返回 jQuery。简单地说return $;,如果没有别的。

例子:

$.fn.plop = function(){
    // Method
    return this.html('bar'); // html() returns jQuery
};

$('#myDiv').plop().delay(800).fadeOut();

http://jsfiddle.net/mUuhF/

于 2013-03-07T18:22:57.877 回答
0

To extend jQuery properly, you need to write your methods like this:

$.fn.plop = function () {  
    return this.each(function () {

        // Do something for each element

    });
};

This will allow you to run .plop() on any collection of jQuery elements, applying the behavior or action to each element, and it will return the original collection so that you may continue to chain additional method calls to it.

于 2013-03-07T18:26:22.100 回答