我只是不知道';无法理解jQuery中的$.fn

我只是不知道';无法理解jQuery中的$.fn,jquery,Jquery,这个语法有什么问题 <script> jQuery(function() { jQuery.fn.myfunction(myparam) { alert(myparam); return 0; // return jQuery? } myfunction('Hello World'); }); </script> jQuery(函数(){ jQuery.fn.myfunction(myparam){ 警报(myp

这个语法有什么问题

<script>
jQuery(function() {
    jQuery.fn.myfunction(myparam) {
        alert(myparam);
        return 0; // return jQuery?
    }
    myfunction('Hello World');
});
</script>

jQuery(函数(){
jQuery.fn.myfunction(myparam){
警报(myparam);
返回0;//返回jQuery?
}
myfunction(“Hello World”);
});

我正在尝试学习如何扩展jQuery。

来自文档

jQuery.fn.extend({
  check: function() {
    return this.each(function() { this.checked = true; });
  },
  uncheck: function() {
    return this.each(function() { this.checked = false; });
  }
});
看更多

应该是

jQuery.fn.myfunction = function(myparam) {

当您为对象
jQuery.fn
的属性
myfunction
赋值
function(){…

时,通过jQuery.myfunction定义的方法是一个“静态”函数。您可以通过执行$.yourFunction调用它,并且它不绑定到结果集


相反,在jQuery.fn.myFunction上定义的函数是与结果集绑定的;如果在该函数中执行“this”,则将获得用于调用它的jQuery对象。换句话说,如果执行$(“p”).myFunction(),则我函数中的“this”将是$(“p”).

您的语法试图调用
jQuery.fn
参考上名为
myFunction
的方法

要扩展jQuery对象,需要以下语法:

<script>
jQuery(function() {
    jQuery.fn.myfunction = function(myparam) {
        alert(myparam);
        return this; // returns the current jQuery object.
    }

    // Select something with jQuery using the $() function here.
    $().myfunction('Hello World');
});
</script>

jQuery(函数(){
jQuery.fn.myfunction=函数(myparam){
警报(myparam);
返回此;//返回当前jQuery对象。
}
//在这里使用$()函数通过jQuery选择一些内容。
$().myfunction('Hello World');
});

Chackey:我来找你寻求帮助,因为我不理解jQuery文档。你想要的是给jQuery.fn.[you_function]一个函数…比如jQuery.fn.example=function(){document.write('example');返回;}…返回此命令将允许您将jQuery对象链接在一起。您可能看到过类似$('#div#').hide().show('slow')的内容;等等。我一直在寻找确切的语法,因为在jQuery中似乎可以做任何你想做的事情——你所需要的只是几行曲线、花括号、大括号和圆括号,然后砰的一声!就这样了。
<script>
jQuery(function() {
    jQuery.fn.myfunction = function(myparam) {
        alert(myparam);
        return this; // returns the current jQuery object.
    }

    // Select something with jQuery using the $() function here.
    $().myfunction('Hello World');
});
</script>