今天用 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裡樣式,但可以以最高優先順序去重新整理樣式。
作者:黎宇浩