多个div崩溃:如果其他元素打开,则关闭一个元素(jQuery)

时间:2013-10-03 23:44:39

标签: javascript jquery toggle togglebutton

HTML

<ul id="slider">
<li>
    <a class="toggle" href="#g1"><img src="BLAHBLAH" /></a>
    <a class="toggle" href="#g2"><img src="BLAHBLAH" style="margin-left:30px;margin-right:30px" /></a>
    <a class="toggle" href="#g3"><img src="BLAHBLAH" /></a>
</li>
<li>
    <a class="toggle" href="#g4"><img src="BLAHBLAH" /></a>
    <a class="toggle" href="#g5"><img src="BLAHBLAH" style="margin-left:30px;margin-right:30px" /></a>
    <a class="toggle" href="#g6"><img src="BLAHBLAH" /></a>
</li>
<li>
    <a class="toggle" href="#g7"><img src="BLAHBLAH" /></a>
    <a class="toggle" href="#g8"><img src="BLAHBLAH" style="margin-left:30px;margin-right:30px" /></a>
    <a class="toggle" href="#g9"><img src="BLAHBLAH" /></a>
</li>
</ul>

<div id="g1" class="gallery"><p>gallery 1</p></div>
<div id="g2" class="gallery"><p>gallery 2</p></div>
<div id="g3" class="gallery"><p>gallery 3</p></div>
...

JS:

<script>
$('.toggle').click(function() {
    var $toggled = $(this).attr('href');
    $($toggled).siblings(':visible').hide("slide", {direction: 'up'}, 750);
    $($toggled).toggle("slide", {direction: 'up'}, 750);
    return false;
});
</script>

CSS:

<style>
.GALLERY { display: none; padding: 50px 20px 20px 20px; }
</style>

小提琴: http://jsfiddle.net/jdrVG/

正如标题所说,如果点击其他切换,我如何关闭一个元素?

提前致谢: - )

1 个答案:

答案 0 :(得分:1)

你可以试试这个。 Demo

 $($toggled).siblings(':visible').hide();