js简单的计算器(使用eval)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var result = document.getElementById("txtResult"); //获取值得ID
            var denghao = document.getElementById("txtDenghao"); //获取等号的ID
            var qingchu = document.getElementById("txtClear");
            //给name是shuzi的加上onclick事件
            var btnshuziArr = document.getElementsByName("shuzi");
            for (var i = 0; i < btnshuziArr.length; i++) {
                btnshuziArr[i].onclick = function () {
                    result.value += this.value;//this代表你点击时的那个
                }
            }
            //结果
            denghao.onclick = function () {
                result.value = eval(result.value);
            }
            //清除
            qingchu.onclick = function () {
                result.value = "";
            }
        }
    </script>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <td colspan="4">
                <input id="txtResult" name="result" type="text" value="" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input id="txtDenghao" name="denghao" type="button" value="=" />
            </td>
            <td colspan="2">
                <input id="txtClear" name="clear" value="清除" type="button" />
            </td>
        </tr>
        <tr>
            <td>
                <input name="shuzi" value="7" type="button" />
            </td>
            <td>
                <input name="shuzi" value="8" type="button" />
            </td>
            <td>
                <input name="shuzi" value="9" type="button" />
            </td>
            <td>
                <input name="shuzi" value="/" type="button" />
            </td>
        </tr>
        <tr>
            <td>
                <input name="shuzi" value="4" type="button" />
            </td>
            <td>
                <input name="shuzi" value="5" type="button" />
            </td>
            <td>
                <input name="shuzi" value="6" type="button" />
            </td>
            <td>
                <input name="shuzi" value="*" type="button" />
            </td>
        </tr>
        <tr>
            <td>
                <input name="shuzi" value="1" type="button" />
            </td>
            <td>
                <input name="shuzi" value="2" type="button" />
            </td>
            <td>
                <input name="shuzi" value="3" type="button" />
            </td>
            <td>
                <input name="shuzi" value="-" type="button" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input name="shuzi" value="0" type="button" />
            </td>
            <td colspan="2">
                <input name="shuzi" value="+" type="button" />
            </td>
        </tr>
    </table>
</body>
</html>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。