jQuery |单击子项,隐藏父项
嘿,我有一个使用 ul 和 li 的列表:
<span class="important">Show/Hide</span>
<div id="important" class="hidden">
<ul class="sub">
<li>
Value one
</li>
<li class="child">
<img src="../img/close.png" />
</li>
</ul>
</div>
$(".important").click(function () {
$("#important").slideToggle("fast");
});
当单击子项(class =“child”)时,它应该向上滑动 div(id =“important”),但是,还有其他具有不同 ID 的列表,我希望在单击子项时 div 向上滑动
我这样做了:
$(".child").click(function () {
$(".child < div").slideUp("fast");
});
我不知道如何使其工作,我已经完成了其他组合,似乎无法做到。
Hey, I have a list of using ul and li:
<span class="important">Show/Hide</span>
<div id="important" class="hidden">
<ul class="sub">
<li>
Value one
</li>
<li class="child">
<img src="../img/close.png" />
</li>
</ul>
</div>
$(".important").click(function () {
$("#important").slideToggle("fast");
});
When the child (class="child") is clicked on, it should slide up the div (id="important"), however, there are other lists that have different IDs, I want the div to slide up when the child is clicked
I did this:
$(".child").click(function () {
$(".child < div").slideUp("fast");
});
I have no idea how to make it work, I've done other combinations, can't seem to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
.closest()
来完成此操作,如下所示从
.child
中,您单击了最整齐的祖先并将其向上滑动。如果您的子级和父级之间可能有其他 div,那么我建议您提供您想要关闭的
类,并更改要查找的最接近的调用特定的
,如下所示:
You can do it using
.closest()
, like thisThis goes from the
.child
you clicked on up the the neatest<div>
ancestor and slides it up. If you may have other divs between the child and parent, then I suggest giving the<div>
you do want closed a class, and changing the closest call to look for that specific<div>
, like this:我会使用 parent() 选择器,这是最好的(我认为)制作某物的方法像这样 :)
I would use the parent() selector, this is the best (my opinion) way to make sth like this :)
我认为您需要“:parent”选择器,在您的情况下它将是:
对于其他选择器,请查看:
http://api.jquery.com/category/selectors/
I think you need the ":parent" selector, in your case it would be:
For other selectors, please have a look at:
http://api.jquery.com/category/selectors/