用指定的类隐藏最近的div

时间:2013-09-21 18:39:02

标签: javascript jquery html css

我正在开发一个模拟桌面样式应用程序窗口的应用程序,该窗口可以打开,最小化和关闭。我设法让他们扩大和崩溃,但我无法接近,我无法弄清楚为什么。我需要能够通过单击按钮关闭当前div,请参阅代码/例如。下方。

<script>
     $(document).ready(function () {

        $("form.common").first().show();
             $(".expand").click(function () {
                 $(this).parents().next("form.common").show();
             })
             $(".collapse").click(function () {
                 $(this).parents().next("form.common").hide();
             });
             $(".close").click(function () {
                 $(this).parents().next("div.module").hide();
             });
    });
 </srcipt>
 <div id="task_manager" class="module"> <!-- Need the x with class close to hide this div-->
 <h3>Task Manager</h3> <!-- This header and below icons are shown when minimized-->
<div class="module_actions">
    <span class="icons_small right close">X</span>
    <span class="icons_small right collapse">-</span>
    <span class="icons_small right expand">+</span> 
</div>  
<form class="common">
    <fieldset>
     <legend> Some Titlee/legend>
    </fieldset>
</form>
</div>

任何人都可以看到为什么这不隐藏div? 谢谢,

3 个答案:

答案 0 :(得分:3)

答案在于问题:

$(".close").click(function () {
    $(this).closest("div.module").hide();
});

<强> Demo fiddle

此外,您应该将通话时的parents()更改为parent(),这样您就可以在DOM树中升级一级

答案 1 :(得分:0)

<中您遗失了<legend> Some Titlee/legend>,之后就可以了。

检查 here

答案 2 :(得分:0)

这应该有效:

  $(".close").click(function () {
       $(this).parent().hide();
   });

JSFiddle
文档: parent()

相关问题