您好,登錄后才能下訂單哦!
這篇文章主要介紹了JS實現簡易計算器的方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
首先創建結構和樣式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body{ margin:0; padding:0; background-color: #abcdef; } .cal{ width:560px; height:100px; margin:0 auto; padding-top:300px; } </style> </head> <body> <div class="cal"> <p> <input type="text" class="num1" value="1"> <span class="sign">+</span> <input type="text" class="num2" value="1"> <span>=</span> <span class="res">2</span> </p> <p> <input type="button" value="+"> <input type="button" value="-"> <input type="button" value="*"> <input type="button" value="/"> </p> </div> <script> </script> </body> </html>
然后添加Java script
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body{ margin:0; padding:0; background-color: #abcdef; } .cal{ width:560px; height:100px; margin:0 auto; padding-top:300px; } </style> </head> <body> <div class="cal"> <p> <input type="text" class="num1" value="1"> <span class="sign">+</span> <input type="text" class="num2" value="1"> <span>=</span> <span class="res">2</span> </p> <p> <input type="button" value="+" class="btn"> <input type="button" value="-" class="btn"> <input type="button" value="*" class="btn"> <input type="button" value="/" class="btn"> </p> </div> <script> var cal=document.querySelector(".cal"); var num1=cal.querySelector(".num1"); var num2=cal.querySelector(".num2"); var sign=cal.querySelector(".sign"); var res=cal.querySelector(".res"); var btns=cal.querySelectorAll(".btn"); btns[0].onclick=add; btns[1].onclick=subtract; btns[2].onclick=multiply; btns[3].onclick=divide; function add(){ sign.innerHTML="+"; //由于DOM獲取到的值都是字符串形式,因此進行操作時需要先轉為number類型 res.innerHTML=Number(num1.value)+Number(num2.value); } function subtract(){ sign.innerHTML="-"; res.innerHTML=Number(num1.value)-Number(num2.value); } function multiply(){ sign.innerHTML="*"; res.innerHTML=Number(num1.value)*Number(num2.value); } function divide(){ sign.innerHTML="/"; res.innerHTML=Number(num1.value)/Number(num2.value); } </script> </body> </html>
代碼的優化:
循環實現綁定
給一個外部接口,用于新增運算
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body{ margin:0; padding:0; background-color: #abcdef; } .cal{ width:560px; height:100px; margin:0 auto; padding-top:300px; } </style> </head> <body> <div class="cal"> <p> <input type="text" class="num1" value="1"> <span class="sign">+</span> <input type="text" class="num2" value="1"> <span>=</span> <span class="res">2</span> </p> <p> <input type="button" value="+" class="btn" title="add"> <input type="button" value="-" class="btn" title="subtract"> <input type="button" value="*" class="btn" title="multiply"> <input type="button" value="/" class="btn" title="divide"> <input type="button" value="%" class="btn" title="mod"> </p> </div> <script> var cal=document.querySelector(".cal"); var num1=cal.querySelector(".num1"); var num2=cal.querySelector(".num2"); var sign=cal.querySelector(".sign"); var res=cal.querySelector(".res"); var btns=cal.querySelectorAll(".btn"); //給每個按鈕綁定事件 for(var i=0;i<btns.length;i++){ operate(i); } //運算函數 function operate(i){ var op=btns[i].value;//獲取運算 var opName=btns[i].title;//獲取運算名 //綁定事件 btns[i].onclick=function(){ sign.innerHTML=op; res.innerHTML=operation[opName](Number(num1.value),Number(num2.value)); } } var operation={ add:function(n1,n2){ return n1+n2; }, subtract:function(n1,n2){ return n1-n2; }, multiply:function(n1,n2){ return n1*n2; }, divide:function(n1,n2){ return n1/n2; }, //給一個新增運算的接口 addOperation:function(name,fn){ //如果該運算不存在 if(!this.name){ this[name]=fn; } } } //新增取余運算 operation.addOperation("mod",function(n1,n2){ return n1%n2; }); </script> </body> </html>
這樣就圓滿完成咯
感謝你能夠認真閱讀完這篇文章,希望小編分享的“JS實現簡易計算器的方法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。