Javascript 回调函数无法访问父函数变量
Javascript callback function not able to access parent function variables
我是 Javascript 的新手,我正在尝试了解我的项目工作的回调,以将文本框输入作为地址,并希望在单击按钮时完成我的核心工作。在 html 这边,我有 N 个文本框,对于来自文本框的每个地址,必须使用相同的功能
window.addEventListener('load', function(){
var variable1;
var variable2;
Buttons = document.getElementsByClassName('Do');
for (var i = 0; i < Buttons .length; i++) {
Buttons [i].addEventListener('click',Do);
}
function Do(event){
let Id = event.target.parentNode.parentNode.id;
let Tag = document.getElementById(Id);
let address = Tag.children[1];
callbackfn.bind(this);
backendlibraryfuntion(callbackfn);
}
function callbackfn(x,y){
console.log(variable1); //working perfectly
console.log(variable2); //working perfectly
console.log(address); // throwing undefined error
}
});
我在回调中访问地址值时遇到问题,我的objective 是获取 N 个地址并根据地址启动 N 个并行实例。有人可以帮助我吗?
在 variable1 和 variable2 附近定义地址变量,这样您就不会遇到这个特定的错误。
var variable1;
var variable2;
let address;
我是 Javascript 的新手,我正在尝试了解我的项目工作的回调,以将文本框输入作为地址,并希望在单击按钮时完成我的核心工作。在 html 这边,我有 N 个文本框,对于来自文本框的每个地址,必须使用相同的功能
window.addEventListener('load', function(){
var variable1;
var variable2;
Buttons = document.getElementsByClassName('Do');
for (var i = 0; i < Buttons .length; i++) {
Buttons [i].addEventListener('click',Do);
}
function Do(event){
let Id = event.target.parentNode.parentNode.id;
let Tag = document.getElementById(Id);
let address = Tag.children[1];
callbackfn.bind(this);
backendlibraryfuntion(callbackfn);
}
function callbackfn(x,y){
console.log(variable1); //working perfectly
console.log(variable2); //working perfectly
console.log(address); // throwing undefined error
}
});
我在回调中访问地址值时遇到问题,我的objective 是获取 N 个地址并根据地址启动 N 个并行实例。有人可以帮助我吗?
在 variable1 和 variable2 附近定义地址变量,这样您就不会遇到这个特定的错误。
var variable1;
var variable2;
let address;