为了账号安全,请及时绑定邮箱和手机立即绑定

范例,参考我的

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title></title>

    <style type="text/css">

        input { width: 60px; }

        div { margin: 10px 0 }

    </style>

    <script type="text/javascript">

        window.onload = function () {


            // 获取元素

            var num1 = document.getElementById('num1');

            var num2 = document.getElementById('num2');

            var ret = document.getElementById('ret');

            var btns = document.getElementsByTagName('button');

            var p = parseInt;




var res=0;

            //为按钮添加点击事件,计算结果

            

            for(var i =0;i<btns.length;i++){

                btns[i].onclick=function(){

                    

                    var v1 =  parseInt(num1.value);

                    var v2 =  parseInt(num2.value);

                //   alert(this.textContent);

                    switch(this.textContent){

                        case "+": res=v1+v2;break;

                        case "-": res=v1-v2 ;break;

                        case "*": res=v1*v2 ;break;

                        case "/": res=v1/v2 ;break;

                    }

                    // alert(parseInt(num1.value));

                     console.log(res.toFixed(3))

                    ret.innerHTML=parseFloat(res.toFixed(3));

                }

            }

            

            

            

            

        }

    </script>

</head>

<body>

<input id="num1" type="text"/>

<input id="num2" type="text"/>

<div>结果:<span id="ret"></span></div>

<button>+</button>

<button>-</button>

<button>*</button>

<button>/</button>

</body>

</html>


正在回答

2 回答

都parseInt了,还parseFloat、toFiexed()?

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

范例,参考我的

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信