标签:script 简单计算器 pre initial title har lang rgba java
上学最近正在学JavaScript,分享一个我写的JavaScript的源码计算器:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>Document</title> 7 <script language="JavaScript"> 8 9 10 11 function init() 12 { 13 14 //window.alert("init"); 15 //nmr; 16 //nmr2; 17 18 window.rclc=0; 19 20 window.mtdClc=0; 21 window.nmr=0; 22 window.nmr2=0; 23 24 window.btn=0; 25 26 window.svCnt=1; 27 28 //alert(window.svCnt); 29 } 30 31 32 33 function wrtChr(value) 34 { 35 if (window.rclc==1) 36 { 37 document.getElementById("txt").value=""; 38 window.rclc=0; 39 } 40 document.getElementById("txt").value+=value; 41 } 42 function sv() 43 { 44 //window.alert("txt2:"+(Number(svCnt)).toString()); 45 //window.alert(); 46 47 //document.getElementById("dv").innerHTML=svCnt.toString(10); 48 49 50 if (window.svCnt==1) 51 { 52 //window.alert("tst"); 53 54 //window.alert(window.nmr); 55 56 window.nmr=Number(document.getElementById("txt").value); 57 58 //window.alert("tst2"); 59 60 document.getElementById("txt").value=""; 61 } 62 else 63 { 64 //window.alert("tst3"); 65 nmr2=Number(document.getElementById("txt").value); 66 } 67 68 } 69 70 function add() 71 { 72 //window.alert("txt"); 73 sv(); 74 75 document.getElementById("txt").value=""; 76 77 if (window.svCnt==1) 78 { 79 window.svCnt=2; 80 } 81 else 82 { 83 //window.svCnt=1; 84 } 85 window.mtdClc=1; 86 window.btn=1; 87 88 } 89 function sb() 90 { 91 sv(); 92 93 document.getElementById("txt").value=""; 94 95 if (window.svCnt==1) 96 { 97 window.svCnt=2; 98 } 99 else 100 { 101 //window.svCnt=1; 102 } 103 window.mtdClc=2; 104 window.btn=1; 105 } 106 function mlt() 107 { 108 sv(); 109 110 document.getElementById("txt").value=""; 111 112 if (window.svCnt==1) 113 { 114 window.svCnt=2; 115 } 116 else 117 { 118 //window.svCnt=1; 119 } 120 window.mtdClc=3; 121 window.btn=1; 122 } 123 function dvd() 124 { 125 sv(); 126 127 document.getElementById("txt").value=""; 128 129 if (window.svCnt==1) 130 { 131 window.svCnt=2; 132 } 133 else 134 { 135 //window.svCnt=1; 136 } 137 window.mtdClc=4; 138 window.btn=1; 139 } 140 141 function eqv() 142 { 143 sv(); 144 145 //window.alert(window.nmr); 146 //window.alert(window.nmr2); 147 148 if (mtdClc==1) 149 { 150 txt.value=window.nmr+window.nmr2; 151 } 152 153 if (mtdClc==2) 154 { 155 txt.value=window.nmr-window.nmr2; 156 } 157 158 if (mtdClc==3) 159 { 160 txt.value=window.nmr*window.nmr2; 161 } 162 163 if (mtdClc==4) 164 { 165 166 document.getElementById("txt").value=window.nmr/window.nmr2; 167 } 168 169 window.svCnt=1; 170 171 window.rclc=1; 172 173 174 } 175 176 177 178 179 180 </script> 181 </head> 182 <body onload="init();"> 183 184 <div id="dv">简单计算器 V1.0 By YoseZang</div> 185 <input type="text" id="txt"></input> 186 <br> 187 188 <table> 189 <tr> 190 <td><button class="btn" value="1" onclick="wrtChr(this.value);">1</button></td> 191 <td><button class="btn" value="2" onclick="wrtChr(this.value);">2</button></td> 192 <td><button class="btn" value="3" onclick="wrtChr(this.value);">3</button></td> 193 </tr> 194 <tr> 195 <td><button class="btn" value="4" onclick="wrtChr(this.value);">4</button></td> 196 <td><button class="btn" value="5" onclick="wrtChr(this.value);">5</button></td> 197 <td><button class="btn" value="6" onclick="wrtChr(this.value);">6</button></td> 198 </tr> 199 <tr> 200 <td><button class="btn" value="7" onclick="wrtChr(this.value);">7</button></td> 201 <td><button class="btn" value="8" onclick="wrtChr(this.value);">8</button></td> 202 <td><button class="btn" value="9" onclick="wrtChr(this.value);">9</button></td> 203 204 </tr> 205 </table> 206 207 208 <button onclick="add();">+</button> 209 <button onclick="sb();">-</button> 210 <button onclick="mlt();">*</button> 211 <button onclick="dvd();">/</button> 212 <button onclick="eqv();">=</button> 213 214 215 216 217 </body> 218 </html>
简单计算器 Html JavaScript By:YoseZang
标签:script 简单计算器 pre initial title har lang rgba java
原文地址:https://www.cnblogs.com/yose/p/14175799.html