如何在 HTML 正文中对引号进行编码?
我是否应该在 HTML 正文中对引号(例如 " 和 ' -> ;Matt 的东西”
和 ’
)进行编码(例如转换
到
Matt’s Stuff
)?我以为我应该这样做,但一位同事说这没什么大不了的,但我找不到任何说这是禁止的?这是编码的最佳实践吗?
Should I encode quotes (such as " and ' -> ”
and ’
) in my HTML body (e.g. convert <p>Matt's Stuff</p>
to <p>Matt’s Stuff</p>
)? I was under the impression I should, but a co-worker said that it was no big deal. I'm dubious but I can't find anything that says it is forbidden. Am I mistaken? Is it a best-practice to encode? Or is it simply useless?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,只有在属性内部时才需要对引号 (") 进行编码,但是为了使 HTML 代码正确(通过 HTML 验证),您应该始终将引号编码为
"
.撇号 (') 在 HTML 中不需要转义。在 XHTML 中,它们应该被编码为
'
。Encoding quotation marks (") is in practice only needed if the're inside an attribute, however for the HTML code to be correct (passing HTML validation), you should always encode quotation marks as
"
.Apostrophes (') don't need escaping in HTML. In XHTML they should be encoded as
'
.如果您希望您的标记可解析为 XML,您将需要对以下内容进行编码:
无论您是否试图使您的代码兼容 XML,一定要在属性中执行此操作。
If you want your markup to be parsable as XML, you'll want to encode the following:
Definitely do this in attributes whether you're trying to make your code XML compliant or not.
通常,除非您将此类值放入标签的属性中(或其他带有引号会导致解析失败的位置),否则不需要这样做。在常规正文中,未编码的文本可以正常工作。
Typicaly such isn't necessary unless you're placing such values into a tag's attribute (or other places where having quote marks would throw off parsing). In regular body text un-encoded will work fine.
不,如果您想在对值声明使用相同引号的属性值声明中使用它们,则只需使用引号(单引号或双引号)的字符引用:
两个标题值都是
符号表示“马特的东西”
。No, you only need to use character references for quotes (single or double) if you want to use them inside an attribute value declaration that uses the same quotes for the value declaration:
Both title values are
The sign says "Matt's Stuff"
.