現在、こちらの記事(参考ページ)をもとに、
Javascriptにて電卓の作成をしています。
しかしながら演算子が連続で押下できてしまいます。
それを阻止したく試行錯誤しましたが、上手く作用しません。
どう直したらいいのでしょうか。
HTML
1<!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script> 6 <link rel="stylesheet" href="stylesheet.css"> 7 <title>電卓</title> 8 </head> 9 10 <body> 11 <form name="calculator"> 12 <table> 13 <tr> 14 <td colspan="4"> 15 <input type="text" class="display" name="display" value="" disabled> 16 </td> 17 </tr> 18 19 <tr> 20 <td><input type="button" value="7" onclick="get_calc(this)"></td> 21 <td><input type="button" value="8" onclick="get_calc(this)"></td> 22 <td><input type="button" value="9" onclick="get_calc(this)"></td> 23 <td><input type="button" value="+" class="operator1" id="operator" onclick="get_calc(this)" disabled></td> 24 </tr> 25 26 <tr> 27 <td><input type="button" value="4" onclick="get_calc(this)"></td> 28 <td><input type="button" value="5" onclick="get_calc(this)"></td> 29 <td><input type="button" value="6" onclick="get_calc(this)"></td> 30 <td><input type="button" value="-" class="operator1" onclick="get_calc(this)"></td> 31 </tr> 32 33 <tr> 34 <td><input type="button" value="1" onclick="get_calc(this)"></td> 35 <td><input type="button" value="2" onclick="get_calc(this)"></td> 36 <td><input type="button" value="3" onclick="get_calc(this)"></td> 37 <td><input type="button" value="*" class="operator1" name="multi_btn" onclick="get_calc(this)"></td> 38 </tr> 39 40 <tr> 41 <td><input type="button" value="0" onclick="get_calc(this)"></td> 42 <td><input type="button" value="00" onclick="get_calc(this)"></td> 43 <td><input type="button" value="." onclick="get_calc(this)"></td> 44 <td><input type="button" value="/" class="operator1" name="div_btn" onclick="get_calc(this)"></td> 45 </tr> 46 47 <tr> 48 <td colspan="2"><input type="button" value="AC" class="operator2" onclick="get_calc(this)"></td> 49 <td colspan="2"><input type="button" value="=" class="operator2" onclick="get_calc(this)"></td> 50 </tr> 51 </table> 52 </form> 53 54 <script src="main.js"></script> 55 </body> 56 </html>
JS
1function get_calc(btn) { 2 if(btn.value == "=") { 3 document.calculator.display.value = eval(document.calculator.display.value); 4 } else if (btn.value == "AC") { 5 document.calculator.display.value = ""; 6 } else { 7 document.calculator.display.value += btn.value; 8 document.calculator.display.multi_btn.value = "*"; 9 document.calculator.display.div_btn.value = "/"; 10 } 11}
押されたボタンが演算子で、直前に入力されたのが演算子だったら
後者を前者と入れ替えるとか?
回答3件
あなたの回答
tips
プレビュー