<input type="text" id="fruit" />無(wú)法輸出結(jié)果
搞了半天,網(wǎng)上找了別的參考,發(fā)現(xiàn)這個(gè)是無(wú)法輸出結(jié)果的吧?然后type='text' id='txt2'單引號(hào)也是有問(wèn)題的貌似。。。。
<!DOCTYPE html>
<html>
<head>
? ?<title> 事件</title>
? ?<script type="text/javascript">
? ? ? ?function count(){
? ? ? ? ? ?//獲取第一個(gè)輸入框的值
? ? ? ? ? ?var num1 = parseInt(document.getElementById ("txt1").value);
? ? ? ? ? ?//獲取第二個(gè)輸入框的值
? ? ? ? ? ?var num2 = parseInt(document.getElementById ("txt2").value);
? ? ? ? ? ?//獲取選擇框的值
? ? ? ? ? ?var op = document.getElementById("select").value;
? ? ? ? ? ?var fruitA = 0;
? ? ? ? ? ?//獲取通過(guò)下拉框來(lái)選擇的值來(lái)改變加減乘除的運(yùn)算法則
? ? ? ? ? ?switch(op)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?case"+" :
? ? ? ? ? ? ? ? ? ?fruitA = num1 + num2;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case"-" :
? ? ? ? ? ? ? ? ? ?fruitA = num1 - num2;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case"*" :
? ? ? ? ? ? ? ? ? ?fruitA = num1 * num2;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case"/" :
? ? ? ? ? ? ? ? ? ?fruitA = num1 / num2;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ?}
? ? ? ? ? ?//設(shè)置結(jié)果輸入框的值
? ? ? ? ? ?document.getElementById("fruit").innerText = fruitA;
? ? ? ?}
? ?</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()"/> <!--通過(guò) = 按鈕來(lái)調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->
<!--<input type="text" id="fruit" />-->
<p ?id="fruit"></p>
</body>
</html>
2015-10-16
<p ?id="fruit"></p> 你這用的p標(biāo)簽,肯定無(wú)法顯示文本文本框了。
<input type="text" id="fruit" />是沒(méi)錯(cuò)的. 具體改變值見(jiàn)樓上的document.getElementById("fruit").value?=?fruitA;
2015-10-16
那是因?yàn)檫@里用了innerText,innerText改變的是元素的文本內(nèi)容。要改變文本框的內(nèi)容應(yīng)該用value。
如
試試看,這樣文本框就能正確顯示了。
單雙引號(hào)沒(méi)問(wèn)題,通用的,注意嵌套就可以了。