【发布时间】:2014-07-16 22:34:58
【问题描述】:
我有这个对象:
Tr.Competence = function() {
var thisCount = //variableNumber;
this.name = "comp" + thisCount;
var newRow = parentTable.insertRow(-1);
var cell1 = newRow.insertCell(0);
var element1 = document.createElement('input');
element1.type = "button";
element1.value = "+";
element1.onclick = function() {
makeSpecialisation(thisCount, this.name)
};
cell1.appendChild(element1);
}
还有这个功能:
function makeSpecialisation(parentCount, parentEl) {
window[parentEl + "SpecialisationCount"]++;
var specialisationName = parentEl + "Spe" + window[parentEl + "SpecialisationCount"] ;
//testing variables with an alert
alert(
"parentCount=" + parentCount +
" & parentEl=" + parentEl +
" & specialisationName=" + specialisationName +
" comXSpecialisationCount=" + window[parentEl + "SpecialisationCount"]
);
}
如果 element1.onclick 直接调用 makeSpecialisation(thisCount, this.nom) (没有匿名函数),那么变量没问题,但在评估后我不能使用它(在每个新的 Tr.Competence 实例调用上) . => 见:http://jsfiddle.net/hq8U3/2/
如果 element1.onclick 使用匿名函数包装器调用 makeSpe[...],我可以随时调用它,但变量是 NaN 或未定义(或 JSFiddle 上的空白)。 => 见:http://jsfiddle.net/hq8U3/3/
我该怎么做才能使我的变量正常工作并且我可以在 onClick 事件中调用它们?
【问题讨论】:
标签: javascript onclick wrapper