简体   繁体   中英

Show/Hide Image DIV

I want the image to act as a toggle so when it's clicked on it will reveal the div with the text.

Here's the CSS class I'm using:

.hidden { display: none; }
.unhidden { display: block; }

and the JS:

 function unhide(divID) {
var item = document.getElementById(divID);
if (item) {
item.className=(item.className=='hidden')?'unhidden':'hidden';
  }
}//

Here's the HTML:

<div class="4u">                    
   <!-- Box -->
   <section class="box box-feature">
      <a href="javascript:unhide('test');" class="image image-full" 
      <img src="images/pic01.jpg" alt="" /></a>
   <div id="test" class="hidden">
       <header>
       <h2>Put something here</h2>
       <span class="byline">Maybe here as well I think</span>
       </header>
<p>Test and more text and more text and more text.</p>
    </div>                          
   </section>
  </div>   

You have a syntax error. Change line 4 to:

  <a href="javascript:unhide('test');" class="image image-full">

Note the > at the end of the line.

Unless you're determined to use vanilla JavaScript, a much easier way would be to use jQuery. Add this to your <head> :

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

And then your a href could be just javascript:$('#test').toggle() and you wouldn't need to define any functions or CSS classes.

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