简体   繁体   中英

Display a div when a user clicks using a javascript function

I need to display on a webpage a div when a user clicks on a button. Does someone know how to do it ?

My code so far :

<body onload ="init()">
      <input type="button" value="Display the div tree" onclick="check();" />

      <script ="text/javascript">

      function check() {
      // I'd like to display on the page the div tree
      }

     </script>

      <div id = "tree" style="display:none"> // I don't want to display it unless the user clicks on the button "Display the div tree"
      </div>      

</body>

thanks,

Bruno

document.getElementById('tree').style.display='';

包括在您的检查功能中

i'm not sure if i understood the question, this seems a bit too easy:

function check() {
  document.getElementById('tree').style.display=''; // or display='block';
}

EDIT :
the reason this dooesn't work for you is an error in your code. please change this line:

<script ="text/javascript">

to

<script type="text/javascript">

and everything wiill be fine. also, you should place the script in the head of your document, but thats not absolutely neccessary.

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