代码:
实例
<!DOCTYPE html> <html> <meta charset="utf-8"> <head> <title>3.迷你计算器</title> </head> <body> <input type="text" name="one" id="one" placeholder="第一个数字"> <select name="js" id="js"> <option value="kong">符号</option> <option value="jia">+</option> <option value="jian">-</option> <option value="cheng">*</option> <option value="chu">/</option> </select> <input type="text" name="two" id="two" placeholder="第二个数字"> <input type="submit" name="submit" id="submit" value="计算" onclick="jisuan();"> <p>结果:<span id="jg"></span></p> <script type="text/javascript"> function jisuan(){ var one = document.getElementById('one') var two = document.getElementById('two') var js = document.getElementById('js') var jg = document.getElementById('jg') if(one.value.length == 0){ jg.innerHTML = '请输入第一个数字' return false }else if(isNaN(one.value)){ jg.innerHTML = '非法数字' return false }else if(two.value.length == 0){ jg.innerHTML = '请输入第二个数字' return false }else if(isNaN(two.value)){ jg.innerHTML = '非法数字' return false }else{ var one = parseFloat(one.value) var two = parseFloat(two.value) } switch(js.value){ case 'kong': jg.innerHTML = '请选择操作类型' break case 'jia': jg.innerHTML = one + two break case 'jian': jg.innerHTML = one - two break case 'cheng': jg.innerHTML = one * two break case 'chu': if(two==0){ jg.innerHTML = '除数不能为零' return false } jg.innerHTML = one / two break } } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
手写代码: