对 HTML 属性使用单引号是否正确?
最近我看到了很多这样的内容:
<a href='http://widget-site-example.com/example.html'>
<img src='http://widget-site-example.com/ross.jpg' alt='Ross's Widget' />
</a>
Is it valid to use singlequotes in HTML? 正如我在上面强调的那样,这也是有问题的,因为你必须转义撇号。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
使用单引号当然是有效的(HTML 4.01,第 3.2 节.2)。 我还没有注意到这种趋势,但也许有一些框架为您访问过的网站提供支持,而这些网站恰好使用单引号进行引用。
It's certainly valid to use single quotes (HTML 4.01, section 3.2.2). I haven't noticed such a trend, but perhaps there's some framework that powers web sites you've visited that happens to quote using single quotes.
我发现,当使用使用双引号字符串文字的编程语言动态生成 HTML 时,使用单引号非常方便。
例如
I find using single quotes is handy when dynamically generating HTML using a programming language that uses double quote string literals.
e.g.
当使用 PHP 生成 HTML 时,执行以下操作可能更容易:
比将字符串与变量与字符串连接起来更容易,因为 PHP 解析双引号字符串中的变量。
When using PHP to generate HTML it can be easier to do something like:
than concatenating a string with a variable with a string, as PHP parses variables in double-quoted strings.
您可能想要使用单引号的另一种情况:在数据属性中存储 JSON 数据:
JSON 对象键必须使用双引号,因此属性本身不能使用双引号。
Another case where you may want to use single quotes: Storing JSON data in data attributes:
JSON object keys must be in double quotes, so the attribute itself cannot.
有人可能会在 PHP 中使用它来避免转义 " 如果他们使用双引号字符串来解析其中的变量,或者避免使用字符串连接运算符。
示例:
而不是
or
现在我总是坚持对 HTML 使用双引号和单引号对于 JavaScript。
Someone may use it in PHP to avoid escaping " if they're using double quoted string to parse variables within it, or to avoid using string concatenation operator.
Example:
instead of
or
Nowadays I always stick to using double quotes for HTML and single quotes for Javascript.
当您想嵌入双引号时更容易。
It's easier when you want to embed double quotes.
在 ASP.NET 中,如果您使用 data-,则使用单引号会更容易属性中的绑定表达式:
In ASP.NET, it's easier to use single quotes if you're using data-binding expressions in attributes:
单引号在 (X)HTML 中是完全合法的。 另一方面,使用反斜杠来转义它们则不然。
是带有 alt 文本“Ross\”的图像,以及空的
s
和Widget
/Widget'
属性。 在 HTML 中转义撇号的正确方法是'
。Single quotes are perfectly legal in (X)HTML. Using a backslash to escape them, on the other hand, isn't.
<img src='http://widget-site-example.com/ross.jpg' alt='Ross\'s Widget' />
is an image with the alt text "Ross\", and emptys
andWidget
/Widget'
attributes. The correct way of escaping an apostrophe in HTML is'
.在 PHP 中,echo 需要多个参数。 因此,如果想省略连接运算符,他们可以执行类似的操作,但仍然使用双引号:
In PHP, echo takes multiples parameters. So, if one would like to omit the concatenation operator, they could done something like and still use double quotes :
单引号会生成更干净、更少混乱的页面。 您不应该在 HTML 字符串中转义它们,使用哪个是您的选择...我的偏好通常是单引号,如果我需要在字符串中包含单引号(例如作为字符串内字符串的分隔符),我用双引号表示 1 & 为对方单身。
Single quotes generate a cleaner page with less clutter. You shouldn't be escaping them in HTML strings and it's your choice which to use... my preference is single quotes normally and if I need to include a single quote in the string (e.g. as delimiters for a string inside the string), I use double quotes for one & single for the other.
如果您希望 html 有效html4 或 xhtml 然后两个 单引号 或 双引号 适用于属性。 HTML4 可以在此处进行验证:https://validator.w3.org/
如果您是 仅支持现代浏览器 (ie10 及更高版本),那么您可以使用 html5 语法允许单引号、双引号和无引号(只要属性值中没有特殊字符)。 HTML5 可以在此处进行验证:https://validator.w3.org/nu
If you want to the html to be valid html4 or xhtml then both single-quotes or double-quotes will work for attributes. HTML4 can be validated here: https://validator.w3.org/
If you are supporting only modern browsers (ie10 and higher) then you can use the html5 syntax which allows single quotes, double quotes, and no quotes (as long as there are no special characters in the attributes' value). HTML5 can be validated here: https://validator.w3.org/nu
什么反对单引号?
你可以在你的 html 代码中使用单/双引号,没有任何问题,只要你在当前标签中保持相同的引用样式(许多浏览器甚至不会抱怨这一点,并且验证只是希望如果你从引用,以相同的结尾,在相同的属性内)
免费支持随机引用样式! (是的;D)
What's against single quotes?
You can have single/double quotes all over your html code without any problem, as long as you keep the same quoting style inside a current tags ( many browser won't complain even about this, and validation just want that if you start with a quote, end with the same, inside the same propriety )
Free support to random quoting styles!!! (yay ;D )
我知道这是一个旧线程,但仍然非常相关。
如果您想控制生成的 HTML 中的引号,可以使用 sprintf() PHP 中的函数(以及许多其他语言中可用的类似调用):
使用 sprintf() 允许通过从数据库或配置文件检索格式字符串或通过翻译机制轻松修改格式字符串。
它的可读性非常好,并且允许在生成的 HTML 中使用双引号或单引号,只需很少的更改并且从不进行任何转义:
I know this is an old thread, but still very much relevant.
If you want control over quotes in your generated HTML, you can use the sprintf() function in PHP (and similar calls available in many other languages):
Using sprintf() allows the format string to be easily modifiable by retrieving it from a database or configuration file, or via translation mechanisms.
It is very readable, and allows either double or single quotes in the generated HTML, with very little change and never any escaping:
为什么不按 SHIFT 键保存。 相同的里程少敲击一次按键。
Why not save pressing the SHIFT Key. One less keystroke for the same milage.
让我们很好地解决这个问题
有趣的是,即使在今天,有些人仍然坚持“仅双引号”标准,而这实际上从来都不是一个标准,但一个约定。 另外,如果您的语言将撇号作为自然元素(英语,有人吗?),那就更方便了,但在我看来,增加 PHP 字符串组合的复杂性为此付出的代价太高了。
Let's settle this argument for good
Funny how some people, even today, stick to the "double quote only" standard which was never really a standard but a convention. Also, if your language has apostrophes as a natural element (English, anyone?) it's more convenient, but in my opinion, increasing the complexity of PHP string compositions is way too high a price to pay for this.
您应该完全避免引号。
在您的示例中,只有一个带引号的属性实际上需要引号。
如果您确实使用引号,则没有硬性规定,但我见过最常见的单引号,必要时在内部使用双引号。
使用双引号不会通过某些验证器。
You should avoid quotes altogether.
In your example only one quoted attribute actually needed quotes.
If you do use quotes, there is no hard and fast rule, but I've seen most commonly single quotes, with double quotes on the inside if necessary.
Using double quotes won't pass some validators.