gpt4 book ai didi

jquery - 切换最近的元素,但在当前类之外

转载 作者:行者123 更新时间:2023-12-01 07:48:25 24 4
gpt4 key购买 nike

在这里,我想隐藏距单击的 msg_head 类最近(之后)的 msg_wrap div。我的jquery:

$(document).on("click", ".msg_head", function() {
$(this).closest('.msg_wrap').slideToggle('slow');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="msg_head" id="c_name_msgHead">
<div class="close">x</div>
</div>

<div class="msg_wrap">wrap
</div>

<div class="msg_head" id="c_name_msgHead">
<div class="close">x</div>
</div>

<div class="msg_wrap">wrap
</div>

但是它不起作用。有什么想法吗?

最佳答案

.closest()查找祖先树:

Description: For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

您需要的是.next() :

Description: Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

$(document).on("click", ".msg_head", function() {
$(this).next('.msg_wrap').slideToggle('slow');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="msg_head" id="c_name_msgHead">
<div class="close">x</div>
</div>

<div class="msg_wrap">wrap
</div>

<div class="msg_head" id="c_name_msgHead">
<div class="close">x</div>
</div>

<div class="msg_wrap">wrap
</div>

关于jquery - 切换最近的元素,但在当前类之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32964345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com