最終的な効果は図2に示すとおりです。バグがあります。整数が正しい後に%をクリックした結果です。小数点後に%をクリックした場合、結果は間違っています!残りは正常です。アドバイスを教えてください。入力の価値は文字列タイプです。 JSの下の図1のIFパーツを正しく処理する方法は? ?
図1
図2
HTMLコードは次のとおりです
<body> <div id = "calculator"> <div> <span> simple calculator </span> <span> @walker </span> </div> <div id = "shuru"> <! - スクリーン入力バー - > <div> <入力タイプ= "id =" screenname "name =" screenname "> </div> <div div div"> < type = "button" id = "7" onclick = "jsq(this.id)" value = "7"> <入力タイプ= "ボタン" id "id" 8 "onclick =" jsq(this.id) "value =" 8 "> <input Type ="ボタン "ID =" 9 "onclick =" jsq(this.id) "" onclick = " value = "back"> <input type = "button" id = "c" onclick = "clearnum()" value = "c" style = "margin-right:0px"> <! - second row-> <入力タイプ= "ボタン" id "id =" 4 "onclick =" jsq(this.id) "値=" 4 " <入力タイプ= "ボタン" id = "6" onclick = "jsq(this.id)" value = "6"> <input type = "button" id "*"*"onclick =" jsq(this.id) "value =" x "> <input Type ="ボタン "ID ="/"onclick =" JSQ(this.id) type = "button" id = "1" onclick = "jsq(this.id)" value = "1"> <input type = "button" id "2" onclick = "jsq(this.id)" value = "2"> <input Type = "ボタン" id = "3" onclick = "jsq(this.id)"値= "3 onclick = "jsq(this.id)" value = "+"> <input type = "button" id "=" - "onclick =" jsq(this.id) "value =" - "style =" marve-right:0px "> <! - 四列 - > <入力タイプ="ボタン "id =" 0 "onclick =" onclick = "jsq(this.id)" value = "00"> <入力型= "ボタン" id = "。"。 onclick = "jsq(this.id)" value = " <a href = "#"ターゲット= "_ blank">フィードバック</a> </span> </div> </div> </body>
CSSコードは次のとおりです。
<style> /*基本的なリセット* /*{マージン:0;パディング:0;ボックスサイズ:ボーダーボックス;フォント:14px arial、sans-serif;} html {height:100%;バックグラウンドカラー:LightsLategrey;}#Calculator {マージン:15px Auto;幅:330px;高さ:400px;ボーダー:1px固体ライトグレイ。バックグラウンドカラー:darkgrey;パディング:15px;}/*ロゴ*/。ロゴ{height:20px;}。logo .name {float:left; line-height:30px;}。logo .verson {float:right; line-height:30px;}/*screen*/#shuru {margin-top:15px;}。スクリーン{マージントップ:5px;幅:300px;高さ:40px;テキストアライグ:右;パディング右:10px; font-size:20px;}#keys {border:1px solid lightgray;高さ:223px;マージントップ:25px;パディング:8px;}#keys .last {margin-right:0px;}。フッター{マージントップ:20px;高さ:20px;}。フッター.link {float:right;}#keys .buttons {float:left;幅:42px;高さ:36px;テキストアライグ:センター;背景色:Lightgray;マージン:0 17px 20px 0;} </style>JavaScriptコードは次のとおりです。
<script> var num = 0; //最初の入力関数jsq(num)のデータを定義します{//現在の入力を取得するif(num == "%"){document.getElementById( 'screenName')。value = math.round(document.getElementbyid( 'screenname')。value)/100; } else {document.getElementById( 'ScreenName')。value += document.getElementById(num).Value; }} function eva(){//入力結果document.getElementById( "screenName")。value = eval(document.getElementById( "screenname")。value); } function clearnum(){// clear 0 document.getelementbyid( "screenname")。value = null; document.getElementById( "screenName")。focus(); } function tuige(){// backspace var arr = document.getElementById( "screenName"); arr.value = arr.value.substring(0、arr.value.length -1); } </script>上記の記事JavaScriptは、単純な計算機効果[推奨]を実装しています。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。