为什么不应该使用 `'` 来转义单引号?

发布于 2024-08-18 22:19:00 字数 637 浏览 3 评论 0 原文

HTML 中的单引号何时变得如此流行? 属性中的 Jquery 嵌入引用HTML 表示以下内容:

单引号字符 (') 在用于引用属性值时,还必须转义为 '' (除 XHTML 文档外,不应转义为 ')当它出现在属性值本身中时。

为什么不应该使用'?另外,使用 " 代替 " 安全吗?

As stated in, When did single quotes in HTML become so popular? and Jquery embedded quote in attribute, the Wikipedia entry on HTML says the following:

The single-quote character ('), when used to quote an attribute value, must also be escaped as ' or ' (should NOT be escaped as ' except in XHTML documents) when it appears within the attribute value itself.

Why shouldn't ' be used? Also, is " safe to be used instead of "?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

手心的海 2024-08-25 22:19:00

" 位于有效 HTML 的官方列表中4 个实体,但 ' 不是。

来自 C.16。命名字符参考'

命名字符引用'
(撇号,U+0027)是
在 XML 1.0 中引入但没有
出现在 HTML 中。作者应该
因此使用 ' 而不是
' 在 HTML 4 中按预期工作
用户代理。

" is on the official list of valid HTML 4 entities, but ' is not.

From C.16. The Named Character Reference ':

The named character reference '
(the apostrophe, U+0027) was
introduced in XML 1.0 but does not
appear in HTML. Authors should
therefore use ' instead of
' to work as expected in HTML 4
user agents.

菩提树下叶撕阳。 2024-08-25 22:19:00

" 在 HTML5 和 HTML4 中均有效。

'HTML5 中有效,但在 HTML4。然而,大多数浏览器无论如何都支持 HTML4 的 '

" is valid in both HTML5 and HTML4.

' is valid in HTML5, but not HTML4. However, most browsers support ' for HTML4 anyway.

高冷爸爸 2024-08-25 22:19:00

' 不是 HTML 4 标准的一部分。

不过," 是可以使用的。

' is not part of the HTML 4 standard.

" is, though, so is fine to use.

狼亦尘 2024-08-25 22:19:00

如果您需要编写语义正确的标记,即使在 HTML5 中,您也不能使用 ' 来转义单引号。虽然,我可以想象你实际上指的是撇号而不是单引号。

单引号撇号在语义上并不相同,尽管它们可能看起来相同。

这是一个撇号。

如果您需要 HTML4 支持,请使用 ' 插入它。 (已编辑)

在英式英语中,单引号的用法如下:

“他告诉我‘尝试一下’”,我说。

报价成对出现。您可以使用:

他告诉我尝试一下,我说。

以语义上正确的方式嵌套引号,将实际字符的替换推迟到渲染引擎。这种替换可能会受到 CSS 规则 的影响,例如

q {
  quotes: '"' '"' '<' '>';
} 

:关于语义正确标记的旧但似乎仍然相关的文章:EM 'n EN(和其他可疑字符)的麻烦< /a>.

(已编辑)这曾经是:

如果您需要 HTML4 支持,请使用 ' 插入它。

但是,正如 @James_pic 指出的那样,这不是直单引号,而是“单弯引号,对吧”。

If you need to write semantically correct mark-up, even in HTML5, you must not use ' to escape single quotes. Although, I can imagine you actually meant apostrophe rather then single quote.

single quotes and apostrophes are not the same, semantically, although they might look the same.

Here's one apostrophe.

Use ' to insert it if you need HTML4 support. (edited)

In British English, single quotes are used like this:

"He told me to 'give it a try'", I said.

Quotes come in pairs. You can use:

<p><q>He told me to <q>give it a try</q></q>, I said.<p>

to have nested quotes in a semantically correct way, deferring the substitution of the actual characters to the rendering engine. This substitution can then be affected by CSS rules, like:

q {
  quotes: '"' '"' '<' '>';
} 

An old but seemingly still relevant article about semantically correct mark-up: The Trouble With EM ’n EN (and Other Shady Characters).

(edited) This used to be:

Use ’ to insert it if you need HTML4 support.

But, as @James_pic pointed out, that is not the straight single quote, but the "Single curved quote, right".

粉红×色少女 2024-08-25 22:19:00

如果你确实需要单引号、撇号,你可以使用

html    | numeric | hex
‘ | ‘  | ‘ // for the left/beginning single-quote and
’ | ’  | ’ // for the right/ending single-quote

If you really need single quotes, apostrophes, you can use

html    | numeric | hex
‘ | ‘  | ‘ // for the left/beginning single-quote and
’ | ’  | ’ // for the right/ending single-quote
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文