zoukankan      html  css  js  c++  java
  • 用javascript加减乘除

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>

    </style>
    </head>
    <body>
    <div>
    <input type="text" size="10" id="num1" />
    <select name="sel" id="sel">
    <option>+</option>
    <option>-</option>
    <option>*</option>
    <option>/</option>
    <option>%</option>
    </select>
    <input type="text" size="10" id="num2" />
    <button onclick="myadd()">=</button>
    <input type="text" size="10" id="num3" readonly />
    <div id="div11" style="color: red; font-size: 12px;" >

    </div>
    </div>
    <script>
    function myadd(){
    var div11 = document.getElementById('div11');
    var num1 = document.getElementById('num1');
    var num2 = document.getElementById('num2');
    var num3 = document.getElementById('num3');
    var sel = document.getElementById('sel');

    div11.innerHTML = "";
    if(num1.value=="" || isNaN(num1.value) || num2.value=="" || isNaN(num2.value)){
    div11.innerHTML = '输入的内容不是数字';
    }else{
    if(sel.value=='+'){
    num3.value = (parseFloat(num1.value) + parseFloat(num2.value)).toFixed(2);
    }else if(sel.value=='-'){
    num3.value = (parseFloat(num1.value) - parseFloat(num2.value)).toFixed(2);
    }else if(sel.value=='*'){
    num3.value = (parseFloat(num1.value) * parseFloat(num2.value)).toFixed(2);
    }else if(sel.value=='/'){
    if(parseFloat(num2.value)==0){
    div11.innerHTML = '被除数不能为0';
    }else{
    num3.value = (parseFloat(num1.value) / parseFloat(num2.value)).toFixed(2);
    }
    }else if(sel.value=='%'){
    if(parseFloat(num2.value)==0){
    div11.innerHTML = '被除数不能为0';
    }else{
    num3.value = (parseFloat(num1.value) % parseFloat(num2.value)).toFixed(2);
    }
    }
    }
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    java学习之实例变量初始化
    rip中的连续子网以及不连续子网
    扫描工具
    WScript.SendKeys()的sendkeys发送组合键以及特殊字符
    sql 查询包含字符的数量统计
    leetcode题1Two sum 练习
    vs 2015密钥
    前端 边界圆角
    前端 字体样式
    前端 高级选择器 伪类选择器
  • 原文地址:https://www.cnblogs.com/ZJBEY/p/6624450.html
Copyright © 2011-2022 走看看