屬性包含選擇器 [name*=”value”]


attributeContains 選擇器

說明: 選取具有指定屬性,且其值包含給定子字串的元素。

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

    attribute: 一個屬性名稱。

    value: 一個屬性值。可以是 有效識別碼 或帶引號的字串。

這是 jQuery 屬性選擇器中,與值匹配最寬鬆的。如果選擇器的字串出現在元素的屬性值中的任何位置,它就會選取該元素。將此選擇器與屬性包含字詞選擇器 (例如 [attr~="word"]) 進行比較,後者在許多情況下更為合適。

範例

找出所有具有包含 'man' 的 name 屬性的輸入,並設定值為一些文字。

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.jquery.com/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>

示範