简易的加减乘除的计算器代码js

发布网友

我来回答

2个回答

热心网友

//html
<input type="text" id="num1" value="" />
    <select id="mySelect">
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
    </select>
    <input type="text" id="num2" value="" />
    <input type="button" id="jisuan" value="计算" />
//js
<script>
    var oTxt1 = document.getElementById('num1');
    var oTxt2 = document.getElementById('num2');
    var oSelect = document.getElementById('mySelect');
    var oBtn = document.getElementById('jisuan');
    oBtn.onclick=function(){
        switch(oSelect.value){
            case '+':
                alert(parseInt(oTxt1.value)+parseInt(oTxt2.value));
                break;
            case '-':
                alert(parseInt(oTxt1.value)-parseInt(oTxt2.value));
                break;
            case '*':
                alert(parseInt(oTxt1.value)*parseInt(oTxt2.value));
                break;
            case '/':
                if(parseInt(oTxt2.value) !== 0){
                    alert(parseInt(oTxt1.value)/parseInt(oTxt2.value));
                }else{
                    alert('除数不能为0');
                }
                
                break;
            default:
                alert('Bug!!!');
        }
    }
</script>

热心网友

OK 搞定! JS代码如下:
function bt()
{
var x,y,z;
x=document.getElementById("ss");
y=document.getElementById("zz");
z=document.getElementById("jj");
h=document.getElementById("yy");
if(h.value=="+")
{
z.value=Number(x.value)+Number(y.value)
}
if(h.value=="-")
{
z.value=Number(x.value)-Number(y.value)

}
if(h.value=="*")
{
z.value=Number(x.value)*Number(y.value)

}
else
{
z.value=Number(x.value)/Number(y.value)

}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com