一次切换一个Div /单击Open Div它将关闭

时间:2015-09-22 15:21:34

标签: javascript html

我试图切换div,这样只有一个div只能一次打开。我已经查看了提供的其他解决方案,但是提供的解决方案是这样的,如果我再次点击open div,它就不会关闭。我正在寻找当前打开的div点击时再次关闭。任何帮助表示赞赏。提前谢谢。

的jsfiddle:http://jsfiddle.net/ZmDs2/78/

HTML

    <table class="table">
        <thead>
            <tr>
                <th>Title</th>
                <th>From</th>
                <th>Utensil</th>
            </tr>
        </thead>

        <tbody>
            <tr class="list">
                <td class="title">Cupcakes</td>
                <td class="from">Molly's Cupcakes</td>
                <td>Chopsticks</td>
            </tr>

            <tr class="description">
                <td>hello </td>
            </tr>


            <tr class="list">
                <td class="title">Pizza</td>
                <td>Roberta's</td>
                <td>Knife</td>
            </tr>

            <tr class="description">
                <td>bye </td>
            </tr>


            <tr class="list">
                <td>Pasta</td>
                <td>Basta Pasta</td>
                <td>Spoon</td>
            </tr>

            <tr class="list">
                <td>Chicken & Waffles</td>
                <td>cell is row 3, column 1</td>
                <td>Spoon</td>
            </tr>

        </tbody>
    </table>

CSS

.description{
    display:none;
}

JS:

    $('.title').on('click', function() {
      var $this = $(this),
          $next = $this.next();

      // Check if another profile is open and close it
      var $last = $('.description:visible', $this.parents('table'));

      if ($last.length) {
        $last.slideUp('fast');
      }

      // Show the new profile content only if we are opening a new profile
      if ($last.parents('.list').index() !== $this.parent().index()) {
        $next.slideDown('fast');
      }
    });

2 个答案:

答案 0 :(得分:0)

除非必要,否则只需避免“重新发明轮子”。引导程序库有一个Collapse元素,可以满足您的需要。

Check it out并查看它是否适合该法案。

答案 1 :(得分:0)

从Twitter Bootstrap库创建了一个collapse元素。

<table class="table table-condensed" style="border-collapse:collapse;">
                    <thead>
                        <tr>
                            <th>Title</th>
                            <th>From</th>
                            <th>Utensil</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr class="list">
                            <td data-toggle="collapse" data-target="#cupcakes" class="accordion-toggle">Cupcakes</td>
                            <td class="from">Molly's Cupcakes</td>
                            <td>Chopsticks</td>
                        </tr>

                        <tr>
                            <td colspan="10" class="hiddenRow"><div class="accordion-body collapse" id="cupcakes">hello</div></td>
                        </tr>


                        <tr class="list">
                            <td data-toggle="collapse" data-target="#pizza" class="accordion-toggle">Pizza</td>
                            <td class="from">Roberta's</td>
                            <td>Knife</td>
                        </tr>

                        <tr>
                            <td colspan="10" class="hiddenRow"><div class="accordion-body collapse" id="pizza">bye</div></td>
                        </tr>


                        <tr class="list">
                            <td data-toggle="collapse" data-target="#pasta" class="accordion-toggle">Pasta</td>
                            <td>Basta Pasta</td>
                            <td>Spoon</td>
                        </tr>

                        <tr>
                            <td colspan="10" class="hiddenRow"><div class="accordion-body collapse" id="pasta">hi</div></tr>
                        </tr>

</tbody>
</table>