JS 實現點擊頁面任意位置隱藏div、span


通過調用下面的 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(""); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM