課程
/前端開發
/JavaScript
/JavaScript進階篇
任務目標:當"職業"選項為默認(即內容為空)時,點擊下邊的文本框彈窗,否則不彈窗。
這個怎么做,利用現有的知識點能做到嗎?我的思路是不是錯了?
2017-04-24
源自:JavaScript進階篇 6-5
正在回答
function?message()?{ var?a?=?document.querySelector('select[name="career"]'); if?(a.value?==?"")?{ alert("請先選擇您現在的職業!"); } }
蘇子晨 提問者
.value
getElementsByClassName獲取的是一個集合
@樓上
我試了id和classname也不行啊,要么全不彈,要么全彈,哪里不對么。
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title> 光標聚焦事件 </title>? <script type="text/javascript">??? function message(){????? var arr=document.getElementsByClassName("opt").innerHTML;????? if(arr=null){?? alert("請先選擇您現在的職業!");?}}? </script></head><body>請選擇您的職業:<br>? <form>??? <select name="career"> ????? <option class="opt" id="nul"></option> ????? <option class="opt">學生</option> ????? <option class="opt">教師</option> ????? <option class="opt">工程師</option> ????? <option class="opt">演員</option> ????? <option class="opt">會計</option> ??? </select></br>??? 您的年齡:</br>??? <input type="text" id="txt" onfocus="message()">? </form></body></html>
要加if判斷,判斷文本框內容是否為空。文本框內容要document.getElementById獲取。
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態交互世界
2 回答光標聚焦事件
4 回答6-5 光標聚集事件
3 回答關于JS進階篇 6-5光標聚焦事件(onfocus)的一些問題
1 回答光標聚焦?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-04-24
2017-04-25
.value
2017-04-24
getElementsByClassName獲取的是一個集合
2017-04-24
@樓上
我試了id和classname也不行啊,要么全不彈,要么全彈,哪里不對么。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 光標聚焦事件 </title>
? <script type="text/javascript">
??? function message(){
????? var arr=document.getElementsByClassName("opt").innerHTML;
????? if(arr=null){
?? alert("請先選擇您現在的職業!");
?}}
? </script>
</head>
<body>
請選擇您的職業:<br>
? <form>
??? <select name="career">
????? <option class="opt" id="nul"></option>
????? <option class="opt">學生</option>
????? <option class="opt">教師</option>
????? <option class="opt">工程師</option>
????? <option class="opt">演員</option>
????? <option class="opt">會計</option>
??? </select></br>
??? 您的年齡:</br>
??? <input type="text" id="txt" onfocus="message()">
? </form>
</body>
</html>
2017-04-24
要加if判斷,判斷文本框內容是否為空。文本框內容要document.getElementById獲取。