无法在div

时间:2017-04-14 00:18:54

标签: javascript html css arrays

每当我点击“=”时,我想在结果和输入按钮下显示所有结果表达式。但我一直在试图解决这个问题。这就是我到目前为止所拥有的。任何帮助将非常感激。谢谢。

<!DOCTYPE html>
<html>
<body>
<head>
</head>
<script>


//Fucntion to bring operation to the operators
function calculation()
{
    var x = parseInt(document.getElementById("op1").value);
    var y = parseInt(document.getElementById("op2").value);
    var z = document.getElementById("operator").value;
    var result;

    if (z == "+"){
        result = x + y;
        document.getElementById("result").value = +result;
    }else if (z == "-"){
        result = x - y;
        document.getElementById("result").value = +result;
    }else if (z == "*"){
        result = x * y;
        document.getElementById("result").value = +result;
    }else if (z == "/"){
        result = x / y;
        document.getElementById("result").value = +result;
    }
    displayResults();
}

function displayResults()
{

    var dispArr = ["document.getElementById('op1').value", "document.getElementById('operator').value", "document.getElementById('op2').value",
    "=","document.getElementById('result').value"];

    dispArr.toString();
    document.getElementbyId("expressions").innerHTML = dispArr.join("");
}

//Function to display the operators
function displayOptr(i) {
    var optrArr =["+","-","*","/"];
    if (i==0){
        document.getElementById("operator").value = "+";
     } else if (i==1){
        document.getElementById("operator").value = "-";
     } else if (i==2){
        document.getElementById("operator").value = "*";
     } else if (i==3){
        document.getElementById("operator").value = "/";    
     }                
}

</script>

<div id="bodyDiv">
        <h1> CALCULATOR </h1>
        <hr/>
        <div class="leftDiv">
            <div id="colorblock">
                <div id = "add" class = "blocks" onClick = "displayOptr(0)">ADD</div>
                <div id = "subtract" class = "blocks" onClick = "displayOptr(1)">SUBTRACT</div>
                <div id = "multiply" class = "blocks" onClick = "displayOptr(2)">MULTIPLY</div>
                <div id = "divide" class = "blocks" onClick = "displayOptr(3)">DIVIDE</div>
            </div>
        </div>
        <div class="rightDiv">
            <div id = "calcblock">
                <input type ="text" size="3" id="op1"/>
                <input type = "text" size="1" id = "operator">
                <input type = "text" size="3"  id="op2"/>
                <input type = "button" value = "="  id="calculate" onClick = "calculation()"/>
                <input type = "text" size="6" id = "result" />
            </div>
        </div>
        <hr/>
        <div class="rightDiv">
            <div id = "pastcalcblock"> 
                <h3> PAST CALCULATIONS </h3> 
                <div id = "resultTab">
                    SORT<br>
                    <input type = "button" value = "As Entered" id = "enteredBut">
                    <input type = "button" value = "By Result" id = "resultBut"><br><br>
                    <div id = "expressions"></hr></div> 

                </div>
            </div>
        </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

dispArr的值在引号中,因此它们被视为字符串。删除引号,然后您在第39行(document.getElementbyId下)使用document.getElementById代替dispArr.toString();

<html>
<body>
<head>
</head>
<script>


//Fucntion to bring operation to the operators
function calculation()
{
    var x = parseInt(document.getElementById("op1").value);
    var y = parseInt(document.getElementById("op2").value);
    var z = document.getElementById("operator").value;
    var result;

    if (z == "+"){
        result = x + y;
        document.getElementById("result").value = +result;
    }else if (z == "-"){
        result = x - y;
        document.getElementById("result").value = +result;
    }else if (z == "*"){
        result = x * y;
        document.getElementById("result").value = +result;
    }else if (z == "/"){
        result = x / y;
        document.getElementById("result").value = +result;
    }
    displayResults();
}

function displayResults()
{

    var dispArr = [document.getElementById('op1').value, document.getElementById('operator').value, document.getElementById('op2').value,
    "=",document.getElementById('result').value];

    dispArr.toString();
    document.getElementById("expressions").innerHTML = dispArr.join("");
}

//Function to display the operators
function displayOptr(i) {
    var optrArr =["+","-","*","/"];
    if (i==0){
        document.getElementById("operator").value = "+";
     } else if (i==1){
        document.getElementById("operator").value = "-";
     } else if (i==2){
        document.getElementById("operator").value = "*";
     } else if (i==3){
        document.getElementById("operator").value = "/";    
     }                
}

</script>

<div id="bodyDiv">
        <h1> CALCULATOR </h1>
        <hr/>
        <div class="leftDiv">
            <div id="colorblock">
                <div id = "add" class = "blocks" onClick = "displayOptr(0)">ADD</div>
                <div id = "subtract" class = "blocks" onClick = "displayOptr(1)">SUBTRACT</div>
                <div id = "multiply" class = "blocks" onClick = "displayOptr(2)">MULTIPLY</div>
                <div id = "divide" class = "blocks" onClick = "displayOptr(3)">DIVIDE</div>
            </div>
        </div>
        <div class="rightDiv">
            <div id = "calcblock">
                <input type ="text" size="3" id="op1"/>
                <input type = "text" size="1" id = "operator">
                <input type = "text" size="3"  id="op2"/>
                <input type = "button" value = "="  id="calculate" onClick = "calculation()"/>
                <input type = "text" size="6" id = "result" />
            </div>
        </div>
        <hr/>
        <div class="rightDiv">
            <div id = "pastcalcblock"> 
                <h3> PAST CALCULATIONS </h3> 
                <div id = "resultTab">
                    SORT<br>
                    <input type = "button" value = "As Entered" id = "enteredBut">
                    <input type = "button" value = "By Result" id = "resultBut"><br><br>
                    <div id = "expressions"></hr></div> 

                </div>
            </div>
        </div>
</body>
</html>