如何在 HTML 正文中对引号进行编码?

发布于 2024-08-10 09:14:03 字数 243 浏览 4 评论 0原文

我是否应该在 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 技术交流群。

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

发布评论

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

评论(4

遥远的绿洲 2024-08-17 09:14:03

实际上,只有在属性内部时才需要对引号 (") 进行编码,但是为了使 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 '.

蹲墙角沉默 2024-08-17 09:14:03

如果您希望您的标记可解析为 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.

弥繁 2024-08-17 09:14:03

通常,除非您将此类值放入标签的属性中(或其他带有引号会导致解析失败的位置),否则不需要这样做。在常规正文中,未编码的文本可以正常工作。

<img src="..." alt="A "quote mark" in an alt attribute" />

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.

<img src="..." alt="A "quote mark" in an alt attribute" />
善良天后 2024-08-17 09:14:03

不,如果您想在对值声明使用相同引号的属性值声明中使用它们,则只需使用引号(单引号或双引号)的字符引用:

title="The sign says "Matt's Stuff""
title='The sign says "Matt's Stuff"'

两个标题值都是 符号表示“马特的东西”

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:

title="The sign says "Matt's Stuff""
title='The sign says "Matt's Stuff"'

Both title values are The sign says "Matt's Stuff".

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