最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - hide div when click outside - Stack Overflow

programmeradmin4浏览0评论

I am using a simple javascript code to toggle div onclick. You can see it in action on this link: k-prim.biz/Untitled-2.html - it is a quite simple demo page. What I want to make is to hide div not only when click on the "link" but also when click outside the div. Also how can I change the css style of the "link" when the div is displayed? Thank you in advance!

<script type="text/javascript" language="javascript"> 
   function showHide() {
   var ele = document.getElementById("showHideDiv");
if(ele.style.display == "block") {
        ele.style.display = "none";
  }
else {
    ele.style.display = "block"; }
}
</script> 
<a href="#" onClick="return showHide();" >link</a>
<div id="showHideDiv" style="display:none;">hello!</div>    

I am using a simple javascript code to toggle div onclick. You can see it in action on this link: k-prim.biz/Untitled-2.html - it is a quite simple demo page. What I want to make is to hide div not only when click on the "link" but also when click outside the div. Also how can I change the css style of the "link" when the div is displayed? Thank you in advance!

<script type="text/javascript" language="javascript"> 
   function showHide() {
   var ele = document.getElementById("showHideDiv");
if(ele.style.display == "block") {
        ele.style.display = "none";
  }
else {
    ele.style.display = "block"; }
}
</script> 
<a href="#" onClick="return showHide();" >link</a>
<div id="showHideDiv" style="display:none;">hello!</div>    
Share Improve this question edited Feb 1, 2013 at 18:33 Ljubo Valevski asked Feb 1, 2013 at 18:26 Ljubo ValevskiLjubo Valevski 892 gold badges2 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You've not given me any code to work with, so you'll have to modify this to suit your needs:

$(document).click(function() {
    if(this != $("#the_div")[0]) {
        $("#the_div").hide();
    }
});

That will hide the div if a user clicks anywhere on the page that isn't that div.

HTML:

<div id="settings">
<input/><select></select><span>text</span>
</div> 

Javascript:

var settings = document.getElementById('settings');

document.onclick = function(e){

    var target = (e && e.target) || (event && event.srcElement);
    var display = 'none';

    while (target.parentNode) {

        if (target == settings) {
            display ='block';
            break;
        }
        target = target.parentNode;
    }

settings.style.display = display;

}

This is a quick hack I wrote. I am not sure why you want to do the same activity by clicking anywhere on the document. If you want to do that, replace jQuery('#link') with jQuery(document).

    <html>
        <head>
           <script type="text/javascript">
               $(document).ready(function(){
                  jQuery('#link').click(function(){
                       if(jQuery('#showHideDiv').hasClass('hide')) {
                          jQuery('#showHideDiv').removeClass('hide');
                          jQuery('#link').css('color', 'red');
                       } else {
                           jQuery('#showHideDiv').addClass('hide');
                           jQuery('#link').css('color', 'blue');
                       }
                    });            
                });
            </script>
        </head>
       <body>
            <a href="#" id="link" >link</a>
            <div id="showHideDiv" class="hide">hello!</div>    
       </body>
    </html>

This is the link to the jsfiddle

http://jsfiddle/EUScV/9/

and also add css rule

   .hide {
        display:none;
    }
发布评论

评论列表(0)

  1. 暂无评论