在CSS属性选择器值中的值需要引用吗? [英] Do values in CSS attribute selector values need to be quoted?

查看:162
本文介绍了在CSS属性选择器值中的值需要引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

  a [href =val] 
pre>

val需要引号吗?单人或双人可以接受吗?对于整数?

解决方案

TLDR:引号 =http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier =nofollow> CSS2.1的标识符规范



CSS规范可能说它们是可选的,但真实世界呈现出不同的故事。当与href属性进行比较时,您需要使用引号(在我非常有限的测试中的单或双工作 - 最新版本的FF,IE,Chrome。)



有趣的是,@Pekka引用的css spec链接恰好使用了href特定示例的引号。



这不仅仅是因为非字母字符,例如句子或斜杠,给出这种独特的情况报价要求 - 使用部分匹配选择器〜=不工作如果你只是在domain.com使用域



好的,每个答案都错了(包括我自己以前的答案。)CSS2规范没有说明在选择器部分本身是否需要引号,但CSS3规范并且将规则引用为CSS21实现:


http://www.w3.org/TR/css3-selectors/


属性值必须是CSS标识符或字符串。 [CSS21]选择器中属性名称和值的区分大小写取决于文档语言。



这里是标识符info:


http:// www .w3.org / TR / CSS21 / syndata.html#value-def-identifier


选择器中的元素名称,类和ID)只能包含字符[a-zA-Z0-9]和ISO 10646字符U + 00A0及更高,以及连字符( - )和下划线(_);它们不能以数字,两个连字符或连字符后跟数字开头。标识符还可以包含转义字符和任何ISO 10646字符作为数字代码(见下一项)。例如,标识符B& W可以写为B \& W \?或B \26 W \3F。



我的回答似乎正确,但这是因为'〜='是一个空格选择器比较器因此它永远不会匹配href值内的部分字符串。 A'* ='比较器工作。而像domain这样的部分字符串适用于匹配href ='www.domain.com'。但检查完整的域名将无法工作,因为它违反了标识符规则。


e.g.:

a[href="val"]

Does "val" need to have quotes around it? Single or double are acceptable? What about for integers?

解决方案

TLDR: Quotes are required unless the value meets the identifier specification for CSS2.1

The CSS spec might say they are optional, but the real world presents a different story. When making a comparison against the href attribute you will need to use quotes (single or double work in my very limited testing - latest versions of FF, IE, Chrome.)

Interestingly enough the css spec link referenced by @Pekka happens to use quotes around their href-specific examples.

And it's not just due to non-alpha characters like the period or slashes that give this unique situation a quote requirement - using a partial match selector ~= doesn't work if you just use the "domain" in "domain.com"

Ok, every answer here is wrong (including my own previous answer.) The CSS2 spec didn't clarify whether quotes are required in the selector section itself, but the CSS3 spec does and quotes the rule as a CSS21 implementation:

http://www.w3.org/TR/css3-selectors/

Attribute values must be CSS identifiers or strings. [CSS21] The case-sensitivity of attribute names and values in selectors depends on the document language.

And here is the identifier info:

http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

My answer seemed correct but that's because the '~=' is a white-space selector comparator so it will never match a partial string inside an href value. A '*=' comparator does work however. And a partial string like 'domain' does work for matching href='www.domain.com'. But checking for a full domain name would not work because it violates the identifier rule.

这篇关于在CSS属性选择器值中的值需要引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆