我正在开发一个应用程序,该应用程序模仿可打开、最小化和关闭的桌面样式应用程序窗口。我已经设法让它们展开和折叠,但我无法关闭它们,我也不知道为什么。我需要能够通过单击按钮关闭当前 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();
});
此外,您应该将对 parents()
的调用更改为仅 parent()
,这样您就可以在 DOM 树中上升一个级别
关于javascript - 隐藏具有指定类的最接近的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18936200/