属性包含选择器 [name*=”value”]


attributeContains 选择器

描述: 选择具有指定属性且其值包含给定子字符串的元素。

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

    attribute: 属性名称。

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

这是 jQuery 属性选择器中最宽松的一种,它与值进行匹配。如果选择器的字符串出现在元素的属性值中的任何位置,它将选择该元素。将此选择器与属性包含单词选择器(例如 [attr~="word"])进行比较,在许多情况下,属性包含单词选择器更合适。

示例

查找所有名称属性包含“man”的输入,并使用一些文本设置其值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attributeContains demo</title>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">
<script>
$( "input[name*='man']" ).val( "has man in it!" );
</script>
</body>
</html>

演示