码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-10-22 22:01:08      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:js

<!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>

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

标签:js

原文地址:http://blog.csdn.net/zhao8912/article/details/40383433

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!