<!doctype html><html><head><meta charset="utf-8"><script type="text/javascript" src="counter.js"></script></head><body>? ?<input type='text' id='txt1' style="width:30px"/>?? ?<select id='select'> <option value='+'>+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option>? ?</select>? ?<input type='text' id='txt2' style="width:30px" />?? ?<input type='button' value=' = ' ?onclick="count()"/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->?? ?<input type='text' id='fruit' /> ?</body></html>function?count(){
???????
????//獲取第一個輸入框的值
????var?vtxt1=document.getElementById("txt1").value;
//獲取第二個輸入框的值
var?vtxt2=document.getElementById("txt2").value;
//獲取選擇框的值
var?symbol=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
var?result;
switch(symbol){
case?"+":
result=parseInt(vtxt1)+parseInt(vtxt2);
break;
case?"-":
result=parseInt(vtxt1)-parseInt(vtxt2);
break;
case?"*":
result=parseInt(vtxt1)*parseInt(vtxt2);
break;
default:
result=parseInt(vtxt1)/parseInt(vtxt2);
}
????//設(shè)置結(jié)果輸入框的值?
????document.getElementById("fruit").value=result;
???}為什么document.getElementById("txt1").value;后面加.value這個字,有什么用啊?為什么要聲明result呢?為什么不可以直接用symbol,比如:symbol=vtxt1+vtxt2;括號里什么時候加引號啊,我看getElementById("txt1")加了引號呢,而后面parseInt(vtxt1)沒有加引號;同理啊,document.getElementById("fruit").value=result;這里啊,這個value有啥用啊?整句話有啥用???case "+":這里為啥要加引號呢?parseInt()有啥用呢?為啥輸出不了輸出小數(shù)?我看有些代碼寫var vtxt2=parseInt(document.getElementById("txt2").value);有些寫在result=parseInt(vtxt1)/parseInt(vtxt2);這個里面,有啥區(qū)別?最后一個弱弱的問題,var的作用域的問題,這個我搞不懂?
想問問簡單的計算器中JS代碼是什么意思?
慕虎9706840
2017-03-15 21:12:04