當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery 屬性包含單詞[name~="value"]用法及代碼示例

用法
attributeContainsWord selector

說明:選擇具有指定屬性的元素,其值包含給定的單詞,由空格分隔。

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

    attribute: 一個屬性名稱。

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

此選擇器將測試字符串與屬性值中的每個單詞進行匹配,其中 "word" 定義為由空格分隔的字符串。如果測試字符串完全等於任何單詞,則選擇器匹配。

例子:

查找具有包含單詞'man' 的名稱屬性的所有輸入,並使用一些文本設置值。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>attributeContainsWord demo</title>
  <script src="https://code.jquery.com/jquery-3.5.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>

演示:

相關用法


注:本文由純淨天空篩選整理自jquery.com大神的英文原創作品 屬性包含單詞[name~="value"]。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。