• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

后代选择器和jQuery的选择器有什么区别

用户头像
it1352
帮助1

问题说明

后代选择器:有选择器

来自文档:


后代选择器

描述:选择作为给定祖先后代的所有元素。

Description: Selects all elements that are descendants of a given ancestor.

元素的后代可以是该元素的子元素,孙子元素,曾孙元素等等。

A descendant of an element could be a child, grandchild, great-grandchild, and so on, of that element.






:has

描述:选择包含至少一个与指定选择器匹配的元素的元素。

Description: Selects elements which contain at least one element that matches the specified selector.

表达式 $('div:has(p)')匹配< div> 如果< p> 存在于其后代中的任何地方,而不仅仅是直接的孩子。

The expression $('div:has(p)') matches a <div> if a <p> exists anywhere among its descendants, not just as a direct child.

即使在阅读了解释之后,差异也不明确。有人可以帮我理解吗?

Even after reading the explanation, the difference is not clear to me. Can someone help me understand?

正确答案

#1

div p 中的后代选择器选择 div p 后代。

The descendant selector in div p selects the p descendants of the div.

div中的has()选择器:has(p) 选择 div 如果它包含任何 p 元素。

The :has() selector in div:has(p) selects the div if it contains any p elements.

大胆的部分是您需要知道的。其余部分可以被视为条件选择这些类型的哪些元素。

The bold parts are all you need to know. The rest can be seen as mere conditions as to which elements of those types are selected.

在CSS选择器术语中,键选择器是最右侧的外部简单选择器。 jQuery(或浏览器的CSS解析器)获取的元素类型是键选择器中的元素。

In CSS selector terms, the key selector is the right-most outer simple selector. The kind of elements that get picked up by jQuery (or by a browser's CSS parser) is the one in the key selector.

在第一个选择器中,键是 p ,因为它是最右边的一个,发生在后代组合子(空间)之后。这意味着将返回 p 元素的集合。

In the first selector, the key is p, because it's the right-most one, occurring after the descendant combinator (the space). This means a collection of p elements will be returned.

的情况下: has(),这是一个伪类, p 是一个内部简单选择器,它是 <$>的一部分c $ c>:has()伪类,不是整个外部选择器的一部分。因此,该选择器中的键是 div ,而不是 p 。这意味着将返回 div 元素的集合,而不是第一个选择器中的 p 元素。

In the case of :has(), which is a pseudo-class, the p is an "inner" simple selector that is part of the :has() pseudo-class, not part of the entire "outer" selector. The key in that selector is therefore div, not p. This means a collection of div elements will be returned, rather than p elements in the first selector.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcckjkj
系列文章
更多 icon
同类精品
更多 icon
继续加载