通過調用下面的 showhidden(“標簽ID”) 顯示div/span/…等標簽內容,可以實現點擊頁面任意地方再次隱藏該標簽內容,而showhidden(“標簽ID”,”nohidden”)可保存顯示不隱藏。可用於仿下拉框及下拉菜單的實現。
var nowshowdiv=""; function showhidden(div,nohidden){ if(div!=""){ if(document.getElementById(div).style.display==""&&nohidden!="nohidden"){ document.getElementById(div).style.display="none"; nowshowdiv=""; /*當nohidden的值為 nohidden 時,div的狀態保持為顯示狀態*/ }else if(nohidden=="nohidden"){ nowshowdiv=""; setTimeout("nowshowdiv='"+div+"'",500); }else{ document.getElementById(div).style.display=""; setTimeout("nowshowdiv='"+div+"'",500); } }else{ if(document.getElementById(nowshowdiv)){
document.getElementById(nowshowdiv).style.display="none"; nowshowdiv=""; } } } document.onclick=function(){ showhidden(""); }