課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
為什么全局變量寫在head會沒用。但是寫在body里就可以用?
2017-07-26
源自:JavaScript進階篇 6-11
正在回答
你知道原因了嗎
幕布斯4301347 提問者
<!DOCTYPE html>
<html>
?<head>
?<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
? <title> 事件</title>
? <script type="text/javascript">
var text1=document.getElementById('txt1');
? var text2=document.getElementById('txt2');
? var text3=document.getElementById('fruit');
? var v = document.getElementById("select");
? ?function a(){
? var bb = v.selectedIndex; // 選中索引
? ? ? ?var aa = v.options[bb].value; // 選中值
? ? ? ?var q=parseInt(text1.value);
? ? ? ?var h=parseInt(text2.value);
? ? ? ?switch(aa){
? ? ? ? ? ?case '+':
? ? ? ? ? ? ? ? text3.value=q+h;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '-':
? ? ? ? ? ? ? ? text3.value=q-h;
? ? ? ? ? ? case '*':
? ? ? ? ? ? ? ? text3.value=q*h;
? ? ? ? ? ? case '/':
? ? ? ? ? ? ? ? text3.value=q/h;
? ? ? ?}
? ?}
? </script> ??
?</head>?
?<body>
? ?<input type='text' id='txt1' />?
? ?<select id='select' value="a">
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
? ?</select>
? ?<input type='text' id='txt2' />?
? ?<input type='button' value=' = ' onclick='a()'/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結果-->?
? ?<input type='text' id='fruit' /> ??
?</body>
</html>
qq_新爺_0
幕布斯4301347 提問者 回復 qq_新爺_0
代碼來看下
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
1 回答js局部變量
1 回答關于局部變量和全局變量
6 回答函數(shù)有全局變量和局部變量之分嗎?
2 回答問下 我定義一個全局變量
1 回答這里面 i 和 hobby 都是全局變量嘛
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2017-08-12
你知道原因了嗎
2017-07-26
<!DOCTYPE html>
<html>
?<head>
?<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
? <title> 事件</title>
? <script type="text/javascript">
var text1=document.getElementById('txt1');
? var text2=document.getElementById('txt2');
? var text3=document.getElementById('fruit');
? var v = document.getElementById("select");
? ?function a(){
? var bb = v.selectedIndex; // 選中索引
? ? ? ?var aa = v.options[bb].value; // 選中值
? ? ? ?var q=parseInt(text1.value);
? ? ? ?var h=parseInt(text2.value);
? ? ? ?switch(aa){
? ? ? ? ? ?case '+':
? ? ? ? ? ? ? ? text3.value=q+h;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '-':
? ? ? ? ? ? ? ? text3.value=q-h;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '*':
? ? ? ? ? ? ? ? text3.value=q*h;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '/':
? ? ? ? ? ? ? ? text3.value=q/h;
? ? ? ? ? ? ? ? break;
? ? ? ?}
? ?}
? </script> ??
?</head>?
?<body>
? ?<input type='text' id='txt1' />?
? ?<select id='select' value="a">
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
? ?</select>
? ?<input type='text' id='txt2' />?
? ?<input type='button' value=' = ' onclick='a()'/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結果-->?
? ?<input type='text' id='fruit' /> ??
?</body>
</html>
2017-07-26
代碼來看下