问题描述
我正在阅读关于的规范,但我找不到任何说如果空格是允许的。我猜这是允许在开始,操作员之前和之后,和在结束。这是正确的吗?
I'm reading the spec on attribute selectors, but I can't find anything that says if whitespace is allowed. I'm guessing it's allowed at the beginning, before and after the operator, and at the end. Is this correct?
推荐答案
属性选择器中的空白规则在语法中说明。以下是属性选择器的产品(一些标记用等效的字符串替换为插图; S *
表示0个或更多空白字符):
The rules on whitespace in attribute selectors are stated in the grammar. Here's the Selectors 3 production for attribute selectors (some tokens substituted with their string equivalents for illustration; S*
represents 0 or more whitespace characters):
attrib
: '[' S* [ namespace_prefix ]? IDENT S*
[ [ '^=' |
'$=' |
'*=' |
'=' |
'~=' |
'|=' ] S* [ IDENT | STRING ] S*
]? ']'
;
当然,语法对于想要了解如何写入的人并不是非常有用属性选择器。
Of course, the grammar isn't terribly useful to someone looking to understand how to write attribute selectors, as it's intended for someone who's implementing a selector engine.
这是一个简单的英语解释:
Here's a plain-English explanation:
这不包括在上面的生产中,但第一个明显的规则是if您要将属性选择器附加到另一个简单选择器或伪元素不要使用空格:
This isn't covered in the above production, but the first obvious rule is that if you're attaching an attribute selector to another simple selector or a pseudo-element, don't use a space:
a[href]::after
如果这样做, a ,属性选择器上隐含的通用选择器以及可能跟随的任何内容它。换句话说,这些选择器彼此相同,但与上述不同:
If you do, the space is treated as a descendant combinator instead, with the universal selector implied on the attribute selector and anything that may follow it. In other words, these selectors are equivalent to each other, but different from the above:
a [href] ::after
a *[href] *::after
属性选择器中的空格
无论你在括号内和比较运算符周围是否有空格都没关系;我发现浏览器似乎对待他们,如果他们不在那里(但我没有测试广泛)。这些都是根据语法有效的,就我所见,在所有现代浏览器中工作:
a[href]
a[ href ]
a[ href="http://stackoverflow.com" ]
a[href ^= "http://"]
a[ href ^= "http://" ]
(显然,打破 ^
和 =
与一个空格不正确。)
(Obviously, breaking the ^
and =
with a space isn't correct.)
如果IE7和IE8正确实现语法,他们应该也能够处理它们。
If IE7 and IE8 implement the grammar correctly, they should be able to handle them all as well.
如果,前缀和属性名称之间不允许有空格。
If a namespace prefix is used, whitespace is not allowed between the prefix and the attribute name.
不正确:
unit[sh| quantity]
unit[ sh| quantity="200" ]
unit[sh| quantity = "200"]
这些都是正确的:
unit[sh|quantity]
unit[ sh|quantity="200" ]
unit[sh|quantity = "200"]
属性值中的空格
围绕上面的属性值;如果您将其舍弃,并尝试选择其属性在其值中包含空格的内容,则会出现语法错误。
Whitespace within the attribute value
But notice the quotes around the attribute values above; if you leave them out, and you try to select something whose attribute has spaces in its value you have a syntax error.
这不正确:
div[class=one two]
$ b b
这是正确的:
This is correct:
div[class="one two"]
这是因为未引用的属性值被视为标识符,不包括空格(显而易见的原因),被视为字符串。有关详情,请参见。
This is because an unquoted attribute value is treated as an identifier, which doesn't include whitespace (for obvious reasons), whereas a quoted value is treated as a string. See this spec for more details.
为了防止此类错误,我强烈建议您始终引用属性值,无论是HTML,XHTML(必需),XML(必需),CSS还是jQuery((或更确切地说,case- em> insensitivity ):
As of Selectors 4, attribute selectors can accept flags in the form of an identifier appearing after the attribute value. So far, one flag has been defined for case-sensitivity (or rather, case-insensitivity):
div[data-foo="bar" i]
语法已经因此:
attrib
: '[' S* attrib_name ']'
| '[' S* attrib_name attrib_match [ IDENT | STRING ] S* attrib_flags? ']'
;
attrib_name
: wqname_prefix? IDENT S*
attrib_match
: [ '=' |
PREFIX-MATCH |
SUFFIX-MATCH |
SUBSTRING-MATCH |
INCLUDE-MATCH |
DASH-MATCH
] S*
attrib_flags
: IDENT S*
在纯英语中:如果属性值没有引用(即它是一个标识符),则需要它和 attrib_flags
之间的空格;否则,如果属性值被引用,则空格是可选的,但强烈建议为了可读性。 attrib_flags
之间的空格和结束括号是一贯可选的。
In plain English: if the attribute value is not quoted (i.e. it is an identifier), whitespace between it and attrib_flags
is required; otherwise, if the attribute value is quoted then whitespace is optional, but strongly recommended for the sake of readability. Whitespace between attrib_flags
and the closing bracket is optional as always.
这篇关于属性选择器中的空格的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!