【发布时间】:2014-11-08 21:58:11
【问题描述】:
我正在尝试将数组添加到我的简单 JavaScript 计算器的 onclick 按钮中。
我的第一次尝试是代码中的process(text[i]);,但它根本不起作用。
所以,我将数组项转换为字符串并提供给 onclick 函数。 然后它以一种奇怪的方式运行。
你能告诉我如何传递给正确的类型吗?
var text = ["+", "-", "*", "/", "SQRT", "POW", "RNDM", "MAX", "MIN"];
for (var i = 0; i < text.length; i++) {
var child = document.createElement('button');
child.innerText = text[i];
child.value = text[i];
var temp = text[i].toString(); //<-- convert to make sure it is string
child.onclick = function () {
process(temp); //<-- it doesn't work properly, and it won't accept "process(text[i])???".
}
label1.appendChild(child);
}
document.body.appendChild(label1);
【问题讨论】:
标签: javascript arrays onclick