標籤:
下拉式清單方塊,滑鼠點擊文字框,出現下拉,滑鼠(離開的時候或者點擊網頁其他位置時)下拉式清單消失。滑鼠放到下拉式清單的某一項上變背景色,點擊下拉式清單的某一項將該項的值顯示在文字框內,然後下拉式清單消失。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><style type="text/css">#a{ width:80px; height:30px; background-color:#CFF; text-align:center; line-height:30px; vertical-align:middle;}#b{ width:80px; height:180px; background-color:#CF9; text-align:center; line-height:30px; vertical-align:middle;}#b div{ text-align:center; line-height:30px; vertical-align:middle;}</style></head><body><div id="q" onclick="wai()"><input id="a" value="菜單" onclick="caidan()" onmouseout="likai()"/> <div id="b" style="display:none" > <div id="c" onmouseover="over(this)" onmouseout="out(this)" onclick="dianji(this)">蘋果</div> <div id="d" onmouseover="over(this)" onmouseout="out(this)" onclick="dianji(this)">梨子</div> <div id="e" onmouseover="over(this)" onmouseout="out(this)" onclick="dianji(this)">山竹</div> <div id="f" onmouseover="over(this)" onmouseout="out(this)" onclick="dianji(this)">桃子</div> <div id="g" onmouseover="over(this)" onmouseout="out(this)" onclick="dianji(this)">核桃</div> <div id="h" onmouseover="over(this)" onmouseout="out(this)" onclick="dianji(this)">香蕉</div> </div></div></body><script type="text/javascript">function caidan(b){ document.getElementById("b").style.display="block"; stopEventBubble(event); //阻止事件冒泡}function over(t){ t.style.backgroundColor="gray"; }function out(y){ y.style.backgroundColor="#CF9"; }function dianji(m){ var n=document.getElementById("a"); n.value = m.innerText;}function stopEventBubble(event){ var e=event || window.event; if (e && e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble=true; } }//隱藏function wai(){ document.getElementById("b").style.display="none"; }</script></html>
JAVAscript——菜單下拉式清單練習