gpt4 book ai didi

javascript - 如何选择下一个div?

转载 作者:数据小太阳 更新时间:2023-10-29 06:12:12 25 4
gpt4 key购买 nike

我在一个页面中多次出现这个部分:

<a class="showComment" style="cursor: pointer;"><i class="icon-comment"></i> Views</a>
<br />
<br />
<div class="writeComment" style="height: auto; width: 700px;" dir="ltr" hidden="hidden">
</div>

我现在正在为a.showComment点击事件在js文件中编写代码。现在我想选择下一个div.writeComment。如何选择它?

最佳答案

在变量 nextDiv 中你可以做任何你想做的事情
使用 .nextAll() 方法允许我们在 DOM 树中搜索这些元素的后继元素,并从匹配元素构造一个新的 jQuery 对象。

使用 .next() 搜索您点击的 DOM 元素试试这个:

 $('.showComment').click(function(){
var nextDiv = $(this).nextAll("div.writeComment");
});

或者

$('.showComment').click(function(){
var nextDiv = $('.showComment').next().find('.writeComment')
});

或者

$('.showComment').click(function(){
var nextDiv = $(this).next("div.writeComment");
});

关于javascript - 如何选择下一个div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047538/

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