Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery插件在没有元素的情况下工作_Javascript_Jquery - Fatal编程技术网

Javascript jQuery插件在没有元素的情况下工作

Javascript jQuery插件在没有元素的情况下工作,javascript,jquery,Javascript,Jquery,如何使jQuery插件能够在不使用元素选择器的情况下执行 比如,通常是: $'myElementID'.myPluginFunctionmyVariables; 但是,相反: $.myPluginFunctionmyVariables 提前谢谢 您可以这样做: $.myPluginFunction = function(vars) { //... }; 您可以这样做: $.myPluginFunction = function(vars) { //... }; 使用$.ext

如何使jQuery插件能够在不使用元素选择器的情况下执行

比如,通常是: $'myElementID'.myPluginFunctionmyVariables; 但是,相反: $.myPluginFunctionmyVariables


提前谢谢

您可以这样做:

$.myPluginFunction = function(vars) {
    //...
};

您可以这样做:

$.myPluginFunction = function(vars) {
    //...
};
使用$.extend。下面是一个小例子:

$(function() {
    return $.extend({
      addDebugBorder: function(selection, size) {
        return selection.each(function() {
          var color, randomColor;
          randomColor = function() {
            return Math.floor(Math.random() * 256);
          };
          if (!size) size = '5px';
          color = 'rgb(' + randomColor() + ',' + randomColor() + ', ' + randomColor() + ')';
          return $(this).css('border', size + ' solid ' + color);
        });
      }
    });
  });
然后像这样叫

$.addDebugBorder($("table tr td"), '1px');
编辑-这个示例代码应该是一个普通的插件,我想我修改了它以使用选择器,而不是只使用div,这就是为什么它有点奇怪。

使用$.extend。下面是一个小例子:

$(function() {
    return $.extend({
      addDebugBorder: function(selection, size) {
        return selection.each(function() {
          var color, randomColor;
          randomColor = function() {
            return Math.floor(Math.random() * 256);
          };
          if (!size) size = '5px';
          color = 'rgb(' + randomColor() + ',' + randomColor() + ', ' + randomColor() + ')';
          return $(this).css('border', size + ' solid ' + color);
        });
      }
    });
  });
然后像这样叫

$.addDebugBorder($("table tr td"), '1px');

编辑-这个示例代码应该是一个普通的插件,我想我修改了它以使用选择器,而不是只处理div,这就是为什么它有点奇怪。

与其将插件分配给jQuery.fn对象,不如将它分配给根jQuery对象本身

jQuery.myPluginFunction=functionmyVariables{ //插件代码 }
与其将插件分配给jQuery.fn对象,不如将其分配给根jQuery对象本身

jQuery.myPluginFunction=functionmyVariables{ //插件代码 } 实际上,您只是使用了$.extend而不是$.fn.extend,请阅读更多信息

$.fn.extend({
   myMethod: function(){...}
});
//jQuery("div").myMethod();

$.extend({
 myMethod2: function(){...}
});
//jQuery.myMethod();
实际上,您只是使用了$.extend而不是$.fn.extend,请阅读更多信息

$.fn.extend({
   myMethod: function(){...}
});
//jQuery("div").myMethod();

$.extend({
 myMethod2: function(){...}
});
//jQuery.myMethod();