jQuery 选择器 |= 在 $("[name|='value']")

jQuery selector |= in $("[name|='value']")

我在一些 javascript 代码中找到了这个片段:

var name = "someString";
var s = "[name|='"+name+"']";
var nArr = $JQ(s);

谁能解释一下 |= 的作用?

我可以在我的调试工具中看到我收到一个 jQuery 元素数组,它们的名称中都有 someString。但是我无法在 jQuery 文档中找到解释。我错过了什么?

这是attribute contains prefix selector

Selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-).

$("a[hreflang|='en']").css("color", "green");
a {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<a href="example.html" hreflang="en">Some text</a>
<a href="example.html" hreflang="en-UK">Some other text</a>
<a href="example.html" hreflang="english">will not be selected</a>


此选择器被引入 CSS 规范以处理语言属性。

W3

Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor.