js 计算器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        input{
            height: 100px;
            width: 500px;
            background-color: dodgerblue;
            text-align: center;
        }
        select{
            height: 106px;
        }
    </style>
</head>
<body>
<input type="text" id="numOne">
<select id="typeSelect">
    <option>+</option>
    <option>-</option>
    <option>*</option>
    <option>/</option>
</select>
<input type="text" id="numTwo">
=
<input type="text" id="sumTxt">
<button id="bn">计算</button>
<script>
    /*
    *  根据id获取标签 document.getElementById("id")
    *  获取下拉菜单选择的索引值 下拉菜单.selectedIndex
    *  input 文本框的内容   文本框.value
    * */
    /*    console.log(typeSelect.selectedIndex);
  console.log(numOne.value);
  sumTxt.value="10";*/
    var numOne=document.getElementById("numOne");
    var numTwo=document.getElementById("numTwo");
    var sumTxt=document.getElementById("sumTxt");
    var typeSelect=document.getElementById("typeSelect");
    var bn=document.getElementById("bn");
    //        当点击按钮时执行里面的语句
    bn.addEventListener("click",clickHandler);
    function clickHandler(e) {
        e = e || window.event;
        //点击后执行这里
//                判断语句和最后内容填入sumText都写在这里。。。。
        numOne1 = Number(numOne.value);
        numTwo2 = Number(numTwo.value);

        /*console.log(typeSelect.selectedIndex);
    console.log(numOne.value);
    console.log(numTwo.value);*/
        if (isNaN(numOne1) || isNaN(numTwo2) || (numTwo2 === 0 && typeSelect.selectedIndex === 3)) {
            console.log("输入的数值有误");
        } else {
            if (typeSelect.selectedIndex === 0) {
                sumTxt.value = numOne1+ numTwo2;
            }
            if (typeSelect.selectedIndex === 1) {
                sumTxt.value = numOne1 - numTwo2;
            }
            if (typeSelect.selectedIndex === 2) {
                sumTxt.value = numOne1* numTwo2;
            }
            if (typeSelect.selectedIndex === 3) {
                sumTxt.value = numOne1 / numTwo2;
            }

        }
    }

</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值