Simply:

(function($){
    $.fn.plugin = function()
    {
        // results in error
        // when called like
        // $.plugin();
    }
})(jQuery);

The error is (copied from Chrome console):

错误是(从Chrome控制台复制):

Uncaught TypeError: Object function ( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context, rootjQuery );
    } has no method 'plugin'

What rules I have to follow in order to make my plugin work without the element selection?

我必须遵循哪些规则才能使我的插件在没有元素选择的情况下工作?

3 个解决方案

#1


6

Simply assign the function directly to $.plugin:

只需将函数直接分配给$ .plugin:

(function($){
    $.plugin = function()
    {
        // ...
    }
})(jQuery);

Of course this won't refer to selected elements anymore, but to jQuery itself.

当然,这不会再引用所选元素,而是引用jQuery本身。

更多相关文章

  1. jQuery捕获被点击元素的ID
  2. Yii - 加载ajax表单元素的用户端验证
  3. 删除后重新插入后保留对元素的引用
  4. jquery中获取相邻元素相关的命令:next()、prev()和siblings()
  5. 如何选择两个h2之间的所有元素
  6. 千呼万唤 HTML 5 (2) - 区块元素
  7. 在中的元素的自定义子弹符号,这是一个普通字符,而不是一个图像
  8. JQuery选择器和操作得到的元素
  9. 是否有更快的方法来遍历HTMLDocument中的每一个元素呢?

随机推荐

  1. HTML语言中img标签的alt属性和title属性
  2. 使用 html5 svg 绘制图形
  3. XSL:包括根本不工作
  4. 在不可见的webbrowser对象中模拟按键C#
  5. jquery将html转换为字符串和html
  6. img在div中不按百分比调整大小
  7. 为什么代码放到DW里运行,和用记事保存为H
  8. cordova HTML5第一次尝试
  9. jQuery SerialScroll:将类添加到活动项
  10. HTml中面试题 ---&&的作用以及||特点