我见过一些 jquery 代码,人们在其中扩展了主对象,例如:
$('someId').myCustomObject();
这可能吗还是我错了? (如果是,怎么做?)
最佳答案
是的,这很容易实现。构建扩展的标准模式是:
(function($) {
$.fn.myCustomObject = function(options) {
var defaults = { ... };
var opts = $.extend(defaults, options);
this.each(function(i) {
... // Act on each item, $(this).
... // Use opts.blah to read merged options.
});
};
})(jQuery);
这允许您在插件中使用“$”,但允许兼容模式。
关于jquery - 在主元素对象上扩展 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/516110/