获得评估的答案以便在显示屏上阅读

时间:2013-11-19 19:57:12

标签: javascript evaluate

只是想知道是否有人可以建议如何在这篇JS的结尾处获得评估的答案,以阅读我创建的显示ID?

function setup() {
var i;
for (i = 0; i <= 9; i++) {
document.getElementById(i).onclick = handleInput;
document.getElementById("operator_*").onclick = handleInput;
document.getElementById("operator_/").onclick = handleInput;
document.getElementById("operator_.").onclick = handleInput;
document.getElementById("operator_-").onclick = handleInput;
document.getElementById("operator_+").onclick = handleInput;

var x;  
document.getElementById("operator_=").onclick = evaluateInput;

}

}

function handleInput(e) {
var s = document.getElementById("display").childNodes[0];
s.nodeValue += e.srcElement.childNodes[0].nodeValue ;
console.log(e.srcElement.id);

}

function evaluateInput(e) {
console.log(eval(document.getElementById("display").childNodes[0].nodeValue));

}

非常感谢......

1 个答案:

答案 0 :(得分:1)

  

您对手头的问题有什么建议吗?

事实上,我这样做:)

DEMO

function setup() {
    var i;
    for (i = 0; i <= 9; i++) {
        document.getElementById(i).onclick = handleInput;
        document.getElementById("operator_*").onclick = handleInput;
        document.getElementById("operator_/").onclick = handleInput;
        document.getElementById("operator_.").onclick = handleInput;
        document.getElementById("operator_-").onclick = handleInput;
        document.getElementById("operator_+").onclick = handleInput;

        //var x;
        document.getElementById("operator_=").onclick = evaluateInput;
    }
}

function handleInput(e) {
    var s = document.getElementById("display");
    s.innerHTML += e.srcElement.childNodes[0].nodeValue;
}

function evaluateInput(e) {
    var s = document.getElementById("display");
    s.innerHTML = eval(document.getElementById("display").childNodes[0].nodeValue);
}

setup();