Correction status:Uncorrected
Teacher's comments:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>计算器</h2> <div> <input type="text"> <select id="option" > <option value="a">+</option> <option value="b">-</option> <option value="c">*</option> <option value="d">/</option> </select> <input type="text"> <button>计算器</button> <hr> <li></li> </div> <script> var a1 = document.getElementsByTagName("input")[0]; var a2 = document.getElementsByTagName("input")[1]; var opt = document.getElementById("option"); for (i=0; i<opt.length; i++); var button = document.getElementsByTagName("button")[0]; button.onclick =function () { var data=0; var data1=0; if(a1.value.length===0 && a2.value.length===0){ alert('不能为空'); a1.focus(); a2.foucs(); return false; }else if ((isNaN(a1.value)) && (isNaN(a2.value))) { alert('只能为数字'); a1.focus(); a2.foucs(); return false; }else { //转换数值 data =parseFloat(a1.value); data1=parseFloat(a2.value); } var opction =opt.value; var zan=0; var falg =''; switch (opction) { case 'a': falg ='+'; zan=data + data1; break; case 'b': falg ='-'; zan=data - data1; break; case 'c': falg ='*'; zan=data * data1; break; case 'd': falg ='/'; if(data1 ===0){ alert('除数不能为0'); return falg; }else{ zan=data / data1; zan =Math.round(zan *100) /100; } break; } var li =document.getElementsByTagName("li")[0]; var p =document.createElement("p"); p.innerText= data1 + falg +data1 + '=' + zan ; li.appendChild(p); } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例