我们先来看看 JavaScript 代码 =>
function ConstructNodes(className,hiddenId,alertMsg,formName){
this.className = className;
this.hiddenId = hiddenId;
this.alertMsg = alertMsg;
this.formName = formName;
}
ConstructNodes.prototype.getId = function(){
var nodes = document.getElementsByClassName(this.className);
for (var i=0;i<nodes.length;i++){
nodes[i].onclick = function(){
var r = confirm(this.alertMsg);
if (r==true){
alert(this.hiddenId); // undefined
} else {
return;
}
};
}
};
var obj = new ConstructNodes("className","hiddenId","Are you sure ?","formName");
obj.getId();
我在这种情况下的问题是在getId的匿名函数下定义的对象是未定义的,我该如何解决这种情况?谢谢