gpt4 book ai didi

javascript - 在jquery中访问sibilings div

转载 作者:行者123 更新时间:2023-12-03 03:11:18 25 4
gpt4 key购买 nike

我想在单击面板页脚时提醒面板标题文本

提前感谢我也尝试过 .val() 但不起作用

$('.panel .panel-footer').on('click',function (e) {
alert($(this).parent('.panel').closest('.panel-heading .panel-title').text());
});
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title">Super Admin</h3>
<div class="right">
on type="button" class="btn-toggle-collapse"><i class="lnr lnr-question-circle"></i></button>
</div>
</div>
<div class="panel-body">
<div>0 Members</div>
</div>
<div class="panel-footer">
<div>View All<i class="lnr lnr-arrow-right pull-right"></i></div>
</div>
</div>

最佳答案

closest更新为find,最接近的会向上而不是向下

$('.panel .panel-footer').on('click',function (e) {
alert( $(this).parent('.panel').find('.panel-title').text() );
});

https://api.jquery.com/closest/它向上遍历 DOM 树

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.

但是您想向下遍历,因为您已经处于父级($(this).parent('.panel'))。

所以我们使用find() https://api.jquery.com/find/向下遍历

Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

关于javascript - 在jquery中访问sibilings div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46939792/

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