简体   繁体   English

jQuery找到第一个有孩子的父母

[英]jQuery find first parent with child

How to find first parent element with nested child by text? 如何通过文本找到带有嵌套子元素的第一个父元素?

I need to check for each element with "name" class if it belongs to propper category with "title" class but the markup has the following structure: 我需要检查每个带有"name"类的元素,如果它属于带有"title"类的属性类别,但是标记具有以下结构:

<div class="row">
  <div class="col">
    <div class="title">
      <span>Cars</span>
    </div>
  </div>
  <div class="col">
    <a>
      <div class="name">Dodge</div>
    </a>
  </div>
  <div class="col">
    <div class="title">
      <span>Food</span>
    </div>
  </div>
  <div class="col">
    <a>
      <div class="name">Apple</div>
    </a>
  </div>
</div>

The dilemma is that I can't apply just .closest() or .prev() methods. 困境是我不能仅应用.closest().prev()方法。

So, I think I need to check if nested "title" element with it's text was the first upper element of a closest parent othervise I will grab redundant section with unrelated title. 因此,我想我需要检查文本中嵌套的"title"元素是否是最接近的父元素的第一个上部元素,否则我将获取标题不相关的多余部分。

$('.name').each(function() {
  if ( $(this).closest('.col:contains("Food")').length ) {
    $(this).css('color', 'red'); // doesn't work
  }
});

closest() is correct here, but it gets the parent of the .name element, whereas the .col which contains Food is the previous sibling, so you need to add a prev() call in there too, like this: closest()是正确的,但是它获得了.name元素的父元素,而包含Food.col是前一个兄弟,因此您也需要在其中添加一个prev()调用,如下所示:

 $('.name').each(function() { if ($(this).closest('.col').prev('.col:contains("Food")').length) { $(this).css('color', 'red'); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <div class="col"> <div class="title"> <span>Cars</span> </div> </div> <div class="col"> <a> <div class="name">Dodge</div> </a> </div> <div class="col"> <div class="title"> <span>Food</span> </div> </div> <div class="col"> <a> <div class="name">Apple</div> </a> </div> </div> 

Alternatively you could invert the logic to find the .col which contains Food and retrieve the related .name . 或者,您可以反转逻辑以找到包含Food.col并检索相关的.name This removes the need for explicit loop and the if condition: 这消除了显式循环和if条件的需要:

 $('.col:contains("Food")').next('.col').find('.name').css('color' , 'red'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <div class="col"> <div class="title"> <span>Cars</span> </div> </div> <div class="col"> <a> <div class="name">Dodge</div> </a> </div> <div class="col"> <div class="title"> <span>Food</span> </div> </div> <div class="col"> <a> <div class="name">Apple</div> </a> </div> </div> 

Using Next Adjacent Selector (“prev + next”) 使用下一个相邻选择器(“上一个+下一个”)

 $('.col:contains("Food") + .col .name').css('color', 'red'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <div class="col"> <div class="title"> <span>Cars</span> </div> </div> <div class="col"> <a> <div class="name">Dodge</div> </a> </div> <div class="col"> <div class="title"> <span>Food</span> </div> </div> <div class="col"> <a> <div class="name">Apple</div> </a> </div> </div> 

Note: Can also be achieved with CSS alone. 注意:也可以仅使用CSS来实现。

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

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