简体   繁体   中英

How to hide div

I need some script.

  1. To hide a div by clicking anywhere on the page
  2. To hide a div by clicking on button, images or particular script
  3. To hide a div by clicking anywhere on the page ( when div contain any other script)

I am currently using

 <script> $(document).ready(function () { $("#div").click(function () { document.getElementById('div').style.display = 'none'; }); }); </script> 

This working fine why div contain images, but it not working when div contain script like advertisement script such as chitika, adsense or any other

Please tell how to do.

Try this use this this selector is used to hide the clicked div

$("#div").click(function () {
          $(this).hide();
      });

To hide it, you would do some javascript and

Also you need to include Jquery library

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
$("#YourDivID").click(function (){     //inspect the ID might differ.
      $(this).hide();
  });

This code answers to your first and third question:

$( document.body ).click(function() {
document.getElementById(name of your div).style.display = 'none';
});

And the second question:

 <div id="testdiv">test</div> <script> function hidediv(){ document.getElementById("testdiv").style.display = 'none'; } </script> <input type=button value="Click me" onclick="hidediv()"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM