[属性~=值]和[属性*=值]之间的区别

发布于 2024-12-09 20:40:06 字数 439 浏览 0 评论 0原文

我找不到这两个选择器之间的区别。两者似乎都做同样的事情,即根据包含给定字符串的特定属性值选择标签。

对于[attribute~=value]http://www.w3schools。 com/cssref/sel_attribute_value_contains.asp

对于[attribute*=value]http://www.w3schools.com/cssref/sel_attr_contain.asp

I cannot find the difference between these two selectors. Both seem to do the same thing i.e select tags based on a specific attribute value containing a given string.

For [attribute~=value] : http://www.w3schools.com/cssref/sel_attribute_value_contains.asp

For [attribute*=value] : http://www.w3schools.com/cssref/sel_attr_contain.asp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

热风软妹 2024-12-16 20:40:06

w3schools 是一个众所周知的不可靠来源,与 W3C 无关。相反,请参阅官方 CSS 标准

[attribute~=value ] 匹配空格分隔列表中的任何条目。
它匹配 attribute="a value b",但不匹配 attribute="a valueb"

[attribute*=value] 匹配任何子字符串。
它匹配 attribute="a value b"attribute="a valueb",但不匹配 attribute="x"

w3schools is a notoriously unreliable source, and not related to the W3C. Instead, consult the official CSS standard:

[attribute~=value] matches any entry in a space-delimited list.
It matches attribute="a value b", but not attribute="a valueb".

[attribute*=value] matches any substring.
It matches attribute="a value b" and attribute="a valueb", but not attribute="x".

沙沙粒小 2024-12-16 20:40:06

根据规范

[att~=val]:表示具有 att 属性的元素,其值为以空格分隔的单词列表,其中之一正好是“val”。如果“val”包含空格,它永远不会代表任何内容(因为单词之间用空格分隔)。此外,如果“val”是空字符串,它永远不会代表任何东西。

[att*=val]:表示具有 att 属性的元素,其值至少包含一个子字符串“val”的实例。如果“val”是空字符串,则选择器不代表任何内容。

因此,主要区别在于 * 意味着 val 可以位于属性值中的任何位置,但在 ~ 的情况下,>val 必须是值的精确部分,可以用空格分隔(如 class 属性)。

您可以在此处查看其实际效果:http://jsfiddle.net/kizu/bPX9n/

[ class*=val] 应用于两个 div,但 [class~=val] 应用于 val 与属性其他部分之间用空格分隔的 div。

According to Specs:

[att~=val]: Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.

[att*=val]: Represents an element with the att attribute whose value contains at least one instance of the substring "val". If "val" is the empty string then the selector does not represent anything.

So, the main difference is that * means that the val may be anywhere in the value of attribute, but in case of ~ the val must me exact part of value which can be separated by whitespaces (like class attribute).

You can see it in action here: http://jsfiddle.net/kizu/bPX9n/

the [class*=val] is applied to both divs, but the [class~=val] is applied to the one where the val is separated by whitespaces from the other parts of an attribute.

南街九尾狐 2024-12-16 20:40:06

请不要使用 w3schools。这是一个糟糕的网站。您可以在此处找到更多有关其不良原因的信息。

您可以在 w3c 上找到有关 CSS3 选择器的良好参考:

E[foo~="bar"] 一个 E 元素,其“foo”属性值是一个列表
空格分隔的值,其中之一完全等于“bar”

E[foo*="bar"] 一个 E 元素,其“foo”属性值包含
子字符串“bar”

http://www.w3.org/TR/selectors/#selectors

Please, DO NOT use w3schools. It's a bad site. You can find more about why it's bad here.

You can find good reference about CSS3 selectors on w3c:

E[foo~="bar"] an E element whose "foo" attribute value is a list of
whitespace-separated values, one of which is exactly equal to "bar"

E[foo*="bar"] an E element whose "foo" attribute value contains the
substring "bar"

http://www.w3.org/TR/selectors/#selectors

只是在用心讲痛 2024-12-16 20:40:06

这就是为什么这里的人不鼓励使用 w3schools.com 作为参考网站。他们网站上对这两个选择器的描述确实不容易区分它们。

更好的资源是 W3C 官方文档——其中的区别相当清楚:http:// /www.w3.org/TR/selectors/

E[foo~="bar"] 一个 E 元素,其“foo”属性值是空格分隔值的列表,其中一个值完全等于“bar”

E[foo*="bar"] 一个 E 元素,其“foo”属性值包含子字符串“bar”

基本上,区别在于 *= 是一个哑通配符;它只会查找匹配的字符串,无论它出现在哪里或其周围是什么,而 ~= 是一个分词通配符;如果匹配值是属性中的不同单词,它将查找匹配值。匹配的单词两侧必须由空格或字符串的开头/结尾包围。

This is why people around here discourage the use of w3schools.com as a reference site. The descriptions of the two selectors on their site really don't make it easy to distinguish them.

A better resource to use would be the official W3C documentation -- it's fairly clear about the difference: http://www.w3.org/TR/selectors/

E[foo~="bar"] an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar"

E[foo*="bar"] an E element whose "foo" attribute value contains the substring "bar"

Basically, the difference is that *= is a dumb wildcard; it will just look for the matching string no matter where it appears or what is around it, whereas ~= is a word-break wildcard; it will look for the matching value provided it is a distinct word within the attribute. The matching word must be surrounded on both sides either by white space or the begining/end of the string.

天生の放荡 2024-12-16 20:40:06

从这个 页面

E[foo~="bar"] 一个 E 元素,其“foo”属性值是由空格分隔的值的列表,其中一个完全等于“bar”

E[foo*="bar"] 一个 E 元素,其“foo”属性值包含子字符串“bar”

From this page:

E[foo~="bar"] an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar"

E[foo*="bar"] an E element whose "foo" attribute value contains the substring "bar"

放肆 2024-12-16 20:40:06

*= 用于子字符串

~= 用于单词搜索

,例如:

"这些是测试单词"

attribute~="est" <= 选定

attribute~="est" <= 未选定(因为“est”不作为单词存在)

检查此链接:http://www.w3schools.com/cssref/css_selectors.asp

*= is for substring

~= is for word search

for example:

"these are test words"

attribute~="est" <= selected

attribute~="est" <= not selected (because "est" doesn't exist as a word)

Check this link: http://www.w3schools.com/cssref/css_selectors.asp

妄司 2024-12-16 20:40:06

来自 jquery 选择器文档

属性包含选择器 [name*="value"]
选择具有指定属性且其值包含给定子字符串的元素。

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

换句话说,使用 ~= 要求“value”是单词/标记,因此不会选择“asdfword”,而“asdf word”会被选择。
使用 *= 只是查找子字符串,因此“asdfword”和“asdf word”都会被选择。

From the jquery selector documentation:

Attribute Contains Selector [name*="value"]
Selects elements that have the specified attribute with a value containing the a given substring.

Attribute Contains Word Selector [name~="value"]
Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.

In other words, using ~= requires "value" to be word/token, so "asdfword" would not be selected, where as "asdf word" would.
Using *= just looks for a substring so both "asdfword" and "asdf word" would be selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文