【发布时间】:2019-02-19 07:16:41
【问题描述】:
问题
我正在开发一个 jQuery 插件。当使用$.fn.bar() 调用时,foo() 中的 alert() 不会输出预期的结果。我在哪里做错了?
(function($){
var input;
var foo = function(){
alert(this.input); //<-- Not getting the desire input value
}
$.fn.bar = function(){
this.input = "Test";
alert(this.input); //<-- Output "Test" as expected
foo(); //<-- Call foo(), expect to alert "Test" as well, but not
};
}(jQuery));
解决方案
您需要将bar() 中的上下文this 传递给foo() 和foo.call(this)。
【问题讨论】:
标签: javascript jquery jquery-plugins