jquery - [属性~=值] 和 [属性*=值] 有什么区别?

标签 jquery jquery-selectors

这两个 jQuery 选择器有什么区别?

以下是来自 w3schools.com 的定义:

  • [attribute~=value] 选择器选择带有 特定属性,其值包含特定字符串。

  • [attribute*=value] 选择器选择带有 特定属性,其值包含字符串。

更新:

以下是来自 jquery.com 的定义。这回答了我的问题:

  • [attribute~=value] - 选择具有指定属性且值包含给定单词(以空格分隔)的元素。

  • [attribute*=value] - 选择具有指定属性且值包含给定子字符串的元素。

最佳答案

*=attributeContains 选择器,来自 jquery docs :

Selects elements that have the specified attribute with a value containing a given substring.

~=attributeContainsWord 选择器,来自 jquery docs :

Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.

参见attributeContains selector with Example of usage hereattributeContainsWord selector with example and usage here

attributeContains 选择器 用于属性值中包含的字符串,而attributeContainsWord 选择器 用于以分隔空格分隔的字符串。官方的jquery例子解释得很清楚。

说明:

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

HTML:

<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">

JQUERY:

$( "input[name*='man']" ).val( "has man in it!" );

输出:

enter image description here

演示示例:

Example of Attribute Contains Selector [name*="value"]

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

HTML:

<input name="man-news">
<input name="milk man">
<input name="letterman2">
<input name="newmilk">

JQUERY:

$( "input[name~='man']" ).val( "mr. man is in it!" );

输出:

enter image description here

演示示例:

Example of Attribute Contains Word Selector [name~="value"]

关于jquery - [属性~=值] 和 [属性*=值] 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26305949/

相关文章:

javascript - 如何在JQuery中为子div自动生成id

jquery - 为什么以下创建 DOM 元素的方法不能在 IE7 和 IE8 中使用 jQuery?

jQuery:在输入隐藏数组中选择特定输入隐藏元素(按索引)

javascript - jQuery 生成 div

javascript - 在 JavaScript 中检查浏览器不活动状态

javascript - iPad、iPhone 模态对话框滚动问题

jquery - 这应该很容易。如何通过按钮的值查找按钮(Jquery 选择器)

javascript - jQuery,如果任何 "one"值发生变化

javascript - 双面锦标赛支架

javascript - 自动幻灯片放映的 javascript 和 jquery 之间有什么区别吗?