課程
/前端開發
/JavaScript
/JavaScript進階篇
新手求6-11計算器的答案
2017-08-14
源自:JavaScript進階篇 6-11
正在回答
<!DOCTYPE html><html>?<head>?? <title> 事件</title> ?? <script type="text/javascript">?? function count(){???? var a = ''; ???? //獲取第一個輸入框的值??? var txt1 = parseInt(document.getElementById("txt1").value);?? ?//獲取第二個輸入框的值?? ?var txt2 = parseInt(document.getElementById("txt2").value);?? ?//獲取選擇框的值?? ?var select = document.getElementById("select").value;?? ??? ?switch(select){?? ???? case "+":?? ??????? a = txt1 + txt2; ?? ???? break;?? ???? case "-":?? ??????? a = txt1 - txt2; ??? ???? break;?? ???? case "*":?? ??????? a = txt1 * txt2; ?? ???? break;?? ???? case "/":?? ??????? a = txt1 / txt2; ??? ?}??? //設置結果輸入框的值 ??? document.getElementById("fruit").value = a;?? }? </script> ?</head> ?<body>?? <input type='text' id='txt1' /> ?? <select id='select'>?? ??? ?<option value='+'>+</option>?? ??? ?<option value="-">-</option>?? ??? ?<option value="*">*</option>?? ??? ?<option value="/">/</option>?? </select>?? <input type='text' id='txt2' /> ?? <input type='button' value=' = ' onclick="count()"/> ?? <input type='text' id='fruit' />? ?</html>
<!DOCTYPE html><html>?<head>? <title> 事件</title> ?? <script type="text/javascript">?? function count(){??? var txt1=Number(document.getElementById("txt1").value);??? var txt2=Number(document.getElementById("txt2").value);?? ?var sel=document.getElementById("select").value;?? ?var sum;?? ?if(sel=="+"){?? ???? sum=txt1+txt2;?? ?}else if(sel=="-"){?? ???? sum=txt1-txt2;?? ?}else if(sel=="*"){?? ???? sum=txt1*txt2;?? ?}else if(sel=="/"){?? ???? sum=txt1/txt2;?? ?}?? ?document.getElementById("fruit").value=sum;?? }? </script> ?</head> ?<body>?? <input type='text' id='txt1' /> ?? <select id='select'>?? ??? ?<option value='+'>+</option>?? ??? ?<option value="-">-</option>?? ??? ?<option value="*">*</option>?? ??? ?<option value="/">/</option>?? </select>?? <input type='text' id='txt2' /> ?? <input type='button' value=' = ' onclick="count();"/> <!--通過 = 按鈕來調用創建的函數,得到結果--> ?? <input type='text' id='fruit' />? ??</body></html>
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態交互世界
1 回答6-11 編程練習
2 回答6-11編程練習
1 回答編程練習6-11
1 回答編程練習6-11我有問題
2 回答Javascript進階篇6-11的編程練習
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-08-14
<!DOCTYPE html>
<html>
?<head>
?? <title> 事件</title> ?
? <script type="text/javascript">
?? function count(){
???? var a = ''; ?
??? //獲取第一個輸入框的值
??? var txt1 = parseInt(document.getElementById("txt1").value);
?? ?//獲取第二個輸入框的值
?? ?var txt2 = parseInt(document.getElementById("txt2").value);
?? ?//獲取選擇框的值
?? ?var select = document.getElementById("select").value;
?? ?
?? ?switch(select){
?? ???? case "+":
?? ??????? a = txt1 + txt2;
?? ???? break;
?? ???? case "-":
?? ??????? a = txt1 - txt2; ?
?? ???? break;
?? ???? case "*":
?? ??????? a = txt1 * txt2;
?? ???? break;
?? ???? case "/":
?? ??????? a = txt1 / txt2; ?
?? ?}
??? //設置結果輸入框的值
??? document.getElementById("fruit").value = a;
?? }
? </script>
?</head>
?<body>
?? <input type='text' id='txt1' />
?? <select id='select'>
?? ??? ?<option value='+'>+</option>
?? ??? ?<option value="-">-</option>
?? ??? ?<option value="*">*</option>
?? ??? ?<option value="/">/</option>
?? </select>
?? <input type='text' id='txt2' />
?? <input type='button' value=' = ' onclick="count()"/>
?? <input type='text' id='fruit' />? ?
</html>
2017-08-18
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
?? function count(){
??? var txt1=Number(document.getElementById("txt1").value);
??? var txt2=Number(document.getElementById("txt2").value);
?? ?var sel=document.getElementById("select").value;
?? ?var sum;
?? ?if(sel=="+"){
?? ???? sum=txt1+txt2;
?? ?}else if(sel=="-"){
?? ???? sum=txt1-txt2;
?? ?}else if(sel=="*"){
?? ???? sum=txt1*txt2;
?? ?}else if(sel=="/"){
?? ???? sum=txt1/txt2;
?? ?}
?? ?document.getElementById("fruit").value=sum;
?? }
? </script>
?</head>
?<body>
?? <input type='text' id='txt1' />
?? <select id='select'>
?? ??? ?<option value='+'>+</option>
?? ??? ?<option value="-">-</option>
?? ??? ?<option value="*">*</option>
?? ??? ?<option value="/">/</option>
?? </select>
?? <input type='text' id='txt2' />
?? <input type='button' value=' = ' onclick="count();"/> <!--通過 = 按鈕來調用創建的函數,得到結果-->
?? <input type='text' id='fruit' />? ?
?</body>
</html>