为$ .plugin()用法准备插件而不是$(selector).plugin()
16lz
2021-01-22
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本身。
更多相关文章
- jQuery捕获被点击元素的ID
- Yii - 加载ajax表单元素的用户端验证
- 删除后重新插入后保留对元素的引用
- jquery中获取相邻元素相关的命令:next()、prev()和siblings()
- 如何选择两个h2之间的所有元素
- 千呼万唤 HTML 5 (2) - 区块元素
- 在中的元素的自定义子弹符号,这是一个普通字符,而不是一个图像
- JQuery选择器和操作得到的元素
- 是否有更快的方法来遍历HTMLDocument中的每一个元素呢?