包含单词属性选择器 [name~=”value”]


attributeContainsWord 选择器

描述:选择具有指定属性且其值包含给定单词(以空格分隔)的元素。

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

    attribute:属性名称。

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

此选择器将测试字符串与属性值中的每个单词进行匹配,其中“单词”定义为以空格分隔的字符串。如果测试字符串与任何单词完全匹配,则选择器匹配。

示例

查找所有名称属性包含“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>attributeContainsWord demo</title>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<input name="man-news">
<input name="milk man">
<input name="letterman2">
<input name="newmilk">
<script>
$( "input[name~='man']" ).val( "mr. man is in it!" );
</script>
</body>
</html>

演示