如何从内部函数更改外部函数变量?
示例:
function ExternalFunction() {
var $element = $('#my-element'); // Some jquery object
var my_variable = 'value';
$element.click(function() {
// Here I want to change "my_variable" value.
// But I can't change it with "my_variable = 'new value';".
})
}
如何在内部功能中更改my_variable?
UPDATE1:
我没有错误,但我无法从内部功能改变变量。请参见示例:
答案 0 :(得分:0)
你不能在DOM内部使用 ExternalFunction (),在DOM准备好之外声明函数
完美无缺,
function ExternalFunction() {
var $element = $('#my-element'); // Some jquery object
var my_variable = 'value';
$('#my-element').on('click', function() {
// Here I want to change "my_variable" value.
// But I can't change it with "my_variable = 'new value';".
my_variable = 'new value';//here we are assigning new value to the variable
console.log(my_variable);
})
}