我想确定 jquery 对象的作用域,这样我就可以覆盖作用域内添加的函数,而无需更改其他地方仍在使用的函数。
这是我想做的事情的一个例子,但它没有按预期工作。有人可以帮忙吗?
$.fn.method = function(){
alert("old method");
}
(function ($) {
//I want to scope functions to jquery in here
$.fn.method = function(){
alert("new Method")
}
$("#MyId").method(); //should alert NEW method | WORKS CORRECTLY!
})(jQuery)
$("#MyId").method(); //should alert OLD method | DOES NOT WORK CORRECTLY
最佳答案
是的,只需将所有内容复制到新对象中即可:
{
const fakeJquery = (...args) => jQuery(...args);
Object.assign(fakeJquery, jQuery);
fakeJquery.fn = {};
(function ($) {
$.fn.method = function(){
alert("new Method")
}
$("#MyId").method();
})(fakeJquery)
}
关于javascript - 是否可以限制 jquery 选择器的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816112/