原生javascript學習:百度IME

來源:互聯網
上載者:User

今天用 javascript 實現了百度IME下拉式功能表
demo:http://cssplusplus.com/demo/js/2_1_baiduInput0.html
原始碼:
01 <!DOCTYPE html>
02 <html>
03 <head>
04 
05 <style>
06 *{margin:0px;padding:0px;}
07 #outer{width:70px;margin:10px auto;font:12px/1.5 Tohoma;}
08 #menu li{list-style-type:none; }
09 li#close{border-top:1px solid #9A99FF;}
10 #menu{margin:6px 0px;border:1px solid #9A99FF;width:68px;display:none;}
11 #menu li a{text-decoration:none;display:block;color: #0000CC;padding:4px 4px;}
12 #menu li a:hover{background:#D9E1F6;;}
13 
14 
15 </style>
16 
17 <script>
18 window.onload = function() {
19     var obtn = document.getElementsByTagName("button")[0];
20     var omenu = document.getElementById("menu");
21     omenu.style.display = "none";
22     obtn.onclick = function() {
23         if (omenu.style.display == "none") {
24             omenu.style.display = "block";
25         } else {
26             omenu.style.display = "none";
27         }
28     }
29 }
30 </script>
31 
32 </head>
33 <body>
34     <div id="outer">
35         <button>IME</button>
36     <ul id="menu">
37         <li><a href="#">手寫</a></li>
38         <li><a href="#">拼音</a></li>
39         <li id="close"><a href="#">關閉</a></li>
40     </ul>
41     </div>
42 </body>
43 </html>
注意如果沒有21行的語句:
1 omenu.style.display = "none";
omenu.style.display將會等於空值,這時點擊button,第一次if判定將是false,進入else的語句,使omenu.style.display = "none"。換言之,必須在首次點擊時,點擊兩次,才能使menu出現。
其實在 javascript 中,對對象使用style方法,不會修改css裡樣式,但可以以最高優先順序去重新整理樣式。

 作者:黎宇浩

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.