我需要 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");
关于javascript - jQuery - 如何在以下子元素中查找元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199398/