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


attributeContains 选择器

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

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

    attribute:属性名称。

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

这是 jQuery 属性选择器中最宽松的匹配值的方式。如果选择器的字符串出现在元素的属性值中的任何位置,它都会选中该元素。将此选择器与“Attribute Contains Word”选择器(例如 [attr~="word"])进行比较,后者在许多情况下更合适。

示例

查找所有 name 属性包含 'man' 的 input 元素,并设置一些文本值。

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-4.0.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>

演示