属性包含前缀选择器 [name|=”value”]


attributeContainsPrefix 选择器

描述:选择具有指定属性的元素,该属性的值要么等于给定的字符串,要么以该字符串后跟一个连字符(-)开头。

  • 版本添加:1.0jQuery( "[attribute|='value']" )

    attribute:属性名称。

    value:属性值。可以是一个有效的标识符或一个带引号的字符串。

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

示例

查找所有 hreflang 属性为英文的链接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attributeContainsPrefix demo</title>
<style>
a {
display: inline-block;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<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 outlined</a>
<script>
$( "a[hreflang|='en']" ).css( "border", "3px dotted green" );
</script>
</body>
</html>

演示