下面是一段简单的计算器代码,我想通过id="result"的DIV显示计算结果,就是不知道该如何写出计算结果。有没有高手讲解一下。
<span>计算器</span><br />
<!-- 输入框 start--><input type="text" id="math" /><!-- 输入框 end-->
<!-- 计算结果显示 start--><div id="result"></div><!-- 计算结果显示 end-->
<br />
<!-- 按钮 start-->
<input type="button" id="count1" value="+" onclick="result_count(0)" />
<input type="button" id="count2" value="-" onclick="result_count(1)" />
<input type="button" id="count3" value="×" onclick="result_count(2)" />
<input type="button" id="count4" value="÷" onclick="result_count(3)" />
<!-- 等于号 start--><input type="button" id="equal" value="=" onclick="equal()" /><br /><!-- 等于号 end-->
<input type="button" id="one" value="1" onclick="math_n(1)" />
<input type="button" id="two" value="2" onclick="math_n(2)" />
<input type="button" id="three" value="3" onclick="math_n(3)" />
<input type="button" id="four" value="4" onclick="math_n(4)" />
<input type="button" id="nine" value="5" onclick="math_n(5)" />
<input type="button" id="six" value="6" onclick="math_n(6)" />
<input type="button" id="seven" value="7" onclick="math_n(7)" />
<input type="button" id="eight" value="8" onclick="math_n(8)" />
<input type="button" id="nine" value="9" onclick="math_n(9)" />
<input type="button" id="zero" value="0" onclick="math_n(0)" />
<input type="button" id="count5" value="." onclick="result_count(4)" />
<script type="text/javascript">
for(m=0;m<=9;m++){
function math_n(m){
document.getElementById("math").value+=m
}
}
var arr = new Array(5)
arr[0] = "+"
arr[1] = "-"
arr[2] = "*"
arr[3] = "/"
arr[4] = "."
for(c=0;c<=4;c++){
function result_count(c){
document.getElementById("math").value+=arr[c]
}
}
</script>
<!-- 按钮 end-->

解决方案 »

  1.   

    document.getElementById('result').innerHTML = xxxx;这样?
      

  2.   

    稍微改了下,红字为修改部分
    <span>计算器</span><br />
    <!-- 输入框 start--><input type="text" id="math" /><!-- 输入框 end-->
    <!-- 计算结果显示 start--><div id="result"></div><!-- 计算结果显示 end-->
    <br />
    <!-- 按钮 start-->
    <input type="button" id="count1" value="+" onclick="result_count(0)" />
    <input type="button" id="count2" value="-" onclick="result_count(1)" />
    <input type="button" id="count3" value="×" onclick="result_count(2)" />
    <input type="button" id="count4" value="÷" onclick="result_count(3)" />
    <!-- 等于号 start--><input type="button" id="equal" value="=" onclick="equal()" /><br /><!-- 等于号 end-->
    <input type="button" id="one" value="1" onclick="math_n(1)" />
    <input type="button" id="two" value="2" onclick="math_n(2)" />
    <input type="button" id="three" value="3" onclick="math_n(3)" />
    <input type="button" id="four" value="4" onclick="math_n(4)" />
    <input type="button" id="nine" value="5" onclick="math_n(5)" />
    <input type="button" id="six" value="6" onclick="math_n(6)" />
    <input type="button" id="seven" value="7" onclick="math_n(7)" />
    <input type="button" id="eight" value="8" onclick="math_n(8)" />
    <input type="button" id="nine" value="9" onclick="math_n(9)" />
    <input type="button" id="zero" value="0" onclick="math_n(0)" />
    <input type="button" id="count5" value="." onclick="result_count(4)" />
    <input type="button" id="reset" value="C" onclick="reset()" />
    <script type="text/javascript">
    for(m=0;m<=9;m++){
    function math_n(m){
    document.getElementById("math").value+=m
    }
    }
    var arr = new Array(5)
    arr[0] = "+"
    arr[1] = "-"
    arr[2] = "*"
    arr[3] = "/"
    arr[4] = "."
    for(c=0;c<=4;c++){
    function result_count(c){
    document.getElementById("math").value+=arr[c]
    }
    }
    function equal()
    {
    document.getElementById('result').innerHTML = eval('('+document.getElementById("math").value+')');
    }
    function reset()
    {
     document.getElementById('result').innerHTML="";
     document.getElementById("math").value="";
     
    }

    </script>
    <!-- 按钮 end-->