JQuery - 显示/隐藏多个div

时间:2011-09-15 17:57:47

标签: jquery show-hide

我的工作“主要是”。我需要为它添加一些额外的功能。在此页面上,有用户打开页面时可见的文本。然后呈现多个“按钮”,这些按钮将与它们相关联。

当您说按esc按钮或单击按钮外部时,我希望“服务”文本变为可见。

这是代码。

    $(document).ready(function() {
    $(".toggles a").click(function(e) {
        var id = this.hash;
            $("#topics div:visible").not(id).fadeOut(function(){
            $(id).fadeIn();
            });
        e.preventDefault();
    });
$("#topics div:not(#services-copy)").hide();
});

这是标记。

<div id="main-content">
<div class="toggles">
<ul>
  <li><a href="#archdesign"> <img src="images/whatwedo/archdesign.jpg" />
    <span>Architectural Design</span>
    </a> </li>
  <li><a href="#landscpdesign"> <img src="images/whatwedo/landscpdesign.jpg" />
    <p>Landscape Architecture</p>
    </a> </li>
  <li><a href="#lpdesign"> <img src="images/whatwedo/lpdesign.jpg" />
    <p>Land Planning</p>
    </a> </li>
  <li><a href="#intdesign"> <img src="images/whatwedo/intdesign.jpg" />
    <p>Interior Design</p>
    </a> </li>
  <li><a href="#gisdesign"> <img src="images/whatwedo/gisdesign.jpg" />
    <p>GIS</p>
    </a> </li>
</ul>
</div>
<div id="topics">
<div id="services-copy">
<h2>Services</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum aliquet eleifend enim nec laoreet. Donec augue felis, volutpat vel eleifend vitae, feugiat eu ipsum. </p>
</div>
<div id="archdesign">
  <h2>Architectural Design</h2>
  <br />
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum aliquet eleifend enim nec laoreet. Donec augue felis, volutpat vel eleifend vitae, feugiat eu ipsum.</p>
</div>
<div id="landscpdesign">
  <h2>Landscape Architecture</h2>
  <br />
  <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum aliquet eleifend enim nec laoreet. Donec augue felis, volutpat vel eleifend vitae, feugiat eu ipsum.</p>
</div>
<div id="lpdesign">
  <h2>Land Planning</h2>
  <br />
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum aliquet eleifend enim nec laoreet. Donec augue felis, volutpat vel eleifend vitae, feugiat eu ipsum.</p>
</div>
<div id="intdesign">
  <h2>Interior Design</h2>
  <br />
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum aliquet eleifend enim nec laoreet. Donec augue felis, volutpat vel eleifend vitae, feugiat eu ipsum.</p>
</div>
<div id="gisdesign">
  <h2>GIS</h2>
  <br />
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum aliquet eleifend enim nec laoreet. Donec augue felis, volutpat vel eleifend vitae, feugiat eu ipsum.</p>
</div>
</div>
</div><!-- End of Main-Content -->

此页面可在Problem Page

查看

任何提示或建议都将深表感谢。

1 个答案:

答案 0 :(得分:0)

function fader(id) {
    $("#topics div:visible").not(id).fadeOut(function() {
        $(id).fadeIn();
    });
}

$(document).ready(function() {
    $(".toggles a").click(function(e) {
        fader(this.hash);
        e.stopPropagation();
        e.preventDefault();
    });
    $("body").click(function(e) {
        fader("#services-copy");
    });
    $(document).keydown(function(e) {
        if (e.keyCode == 27) {
            fader("#services-copy");
        }
    });

    $("#topics div:not(#services-copy)").hide();
});

http://jsfiddle.net/gfzpZ/4/