jQuery-如何在下面的子元素中找到元素

jQuery - how to find element inside following child?

本文关键字:元素 在下面 jQuery-      更新时间:2023-09-26

我需要jQuery语句在以下代码中查找".findme":

$('.clicky').click(function() {
    // the .find() method doesn't do it
    $(this).find('.findme').css('color', 'red');
});
<div class='clicky'></div>
<div class='brother'>
    <div class='findme'>Here I am</div>
</div>

我试过.find().next().children()的变体,但我不能完全解决。。。。

$(this).find('.findme')中的this实际上是指被单击的元素(类为clicky.的div

.find()实际上搜索您调用它的元素的子代,由于"findme"不在"clicky"div中,所以它找不到它

您应该使用jquery的.next()函数来获取紧跟在"clicky"div(即"brother")之后的同级,然后在那里搜索"findme"div。

$(this).next().find(".findme").css("color", "red");

您可以尝试更改

$('.clicky').click(function() {
    // the .find() method doesn't do it
   $(this).siblings().children('.findme').css('color', 'red');
});

我想你错过了div 的点击我的内部文本

<div class='clicky'>click me!!!</div>
<div class='brother'>
  <div class='findme'>Here I am</div>
</div>