简体   繁体   English

将课程添加到最近的孩子

[英]add class to closest child

This question has been asked earlier , but I am still not getting the answer 这个问题已经提过,但是我仍然没有得到答案
Question is if element contains text , add the class. 问题是元素是否包含text,添加类。 But there are 2 elements with same class. 但是有2个元素具有相同的类。
For Example 例如

<div class="xyz">123</div>
<div class="add">...</div>


<div class="xyz"></div>
<div class="add">...</div>

Why doesnt this work? 为什么不起作用?

if ($('div.xyz:contains("123")').length > 0) {
$(this).next(".add").addClass("long");
}

Try like this 这样尝试

$('div.xyz:contains("123")').each(function(){
  $(this).next(".add").addClass("long");
})

DEMO

简化版也应该可以

$('div.xyz:contains("123") +.add ').addClass("long");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM