Select 元素有两个特定的 children 使用 jQuery

Select element has two specific children using jQuery

我想 select 通过匹配两个 children parent。 我的代码:

$('span:contains("11:00am"), span.name:contains("Tom")').parents("a").css("background-color","rgb(255, 255, 255)");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a style="background-color: rgb(0, 0, 0);">
  <div class="time">
    <span>11:00am</span>
  </div>
  <span class="name">Tom</span>
</a>
<a style="background-color: rgb(0, 0, 0);">
  <div class="time">
    <span>11:00am</span>
  </div>
  <span class="name">Jack</span>
</a>

然而,这将 select TomJack 的时间。

$('span:contains("11:00am"),span:contains("Jack")').parent("a").css("background-color", "Grey");
$('span:contains("11:00am"),span:contains("Tom")').parent("a").css("background-color", "Green");
<a style="background-color: rgb(255, 255, 255);">
  <div class="time">
    <span>11:00am</span>
  </div>
  <span class="name">Tom</span>
</a>

<a style="background-color: rgb(255, 255, 255);">
  <div class="time">
    <span>11:00am</span>
  </div>
  <span class="name">Jack</span>
</a>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

您需要 select a 标记,然后使用 :has() select 或过滤具有特定子元素的元素。

$('a:has(span:contains("11:00am")):has(span.name:contains("Tom"))')

$('a:has(span:contains("11:00am")):has(span.name:contains("Tom"))').css("background-color","rgb(255, 255, 255)");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a style="background-color: rgb(0, 0, 0);">
  <div class="time">
    <span>11:00am</span>
  </div>
  <span class="name">Tom</span>
</a>
<a style="background-color: rgb(0, 0, 0);">
  <div class="time">
    <span>11:00am</span>
  </div>
  <span class="name">Jack</span>
</a>