<!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>
时间: 2024-10-03 23:02:46