javascript - 隐藏具有指定类的最接近的 div

标签 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? 谢谢,

最佳答案

答案在问题中:

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

Demo fiddle

此外,您应该将对 parents() 的调用更改为仅 parent(),这样您就可以在 DOM 树中上升一个级别

关于javascript - 隐藏具有指定类的最接近的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18936200/

相关文章:

javascript - 将数据传递给点击监听器

html - 复选框在 div 标签内不对齐

javascript - 是否可以覆盖内联 css 并使内联 css 属性不起作用?

javascript - 在 AngularJs 中调用函数 onclick

javascript - 为什么此站点在注册点击事件时遇到问题?

javascript - 切换后菜单消失

javascript - 将一个数组插入同一个数组javascript

javascript - 如何手动触发从 href 下载文件

javascript - DataTables 日期搜索和重置输入

javascript - Chrome webkitStorageInfo.requestQuota