【问题标题】:Show/Hide Image DIV显示/隐藏图像 DIV
【发布时间】:2013-07-16 01:13:55
【问题描述】:

我希望图像充当切换开关,因此当单击它时,会显示带有文本的 div。

这是我正在使用的 CSS 类:

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

和 JS:

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

这是 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>   

【问题讨论】:

  • 运行时会发生什么?如果您按原样粘贴代码,则会出现问题,因为您的锚标记上没有&gt;。

标签: javascript html css show-hide


【解决方案1】:

您有语法错误。将第 4 行更改为:

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

注意行尾的&gt;。

除非您决定使用原生 JavaScript,否则更简单的方法是使用 jQuery。将此添加到您的&lt;head&gt;:

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

然后你的a href 可能只是javascript:$('#test').toggle(),你不需要定义任何函数或 CSS 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多