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