亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想問問簡單的計算器中JS代碼是什么意思?

想問問簡單的計算器中JS代碼是什么意思?

慕虎9706840 2017-03-15 21:12:04
<!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()"/> <!--通過 = 按鈕來調用創建的函數,得到結果-->?? ?<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); } ????//設置結果輸入框的值? ????document.getElementById("fruit").value=result; ???}為什么document.getElementById("txt1").value;后面加.value這個字,有什么用?。繛槭裁匆暶鱮esult呢?為什么不可以直接用symbol,比如:symbol=vtxt1+vtxt2;括號里什么時候加引號啊,我看getElementById("txt1")加了引號呢,而后面parseInt(vtxt1)沒有加引號;同理啊,document.getElementById("fruit").value=result;這里啊,這個value有啥用???整句話有啥用???case "+":這里為啥要加引號呢?parseInt()有啥用呢?為啥輸出不了輸出小數?我看有些代碼寫var vtxt2=parseInt(document.getElementById("txt2").value);有些寫在result=parseInt(vtxt1)/parseInt(vtxt2);這個里面,有啥區別?最后一個弱弱的問題,var的作用域的問題,這個我搞不懂?
查看完整描述

1 回答

  • 1 回答
  • 1 關注
  • 1255 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號