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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2083754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:52:13  来源:igfitidea点击:

Why shouldn't `'` be used to escape single quotes?

htmlescapingquotes

提问by brad

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:

正如HTML 中的单引号何时变得如此流行?Jquery 在属性中嵌入了引用,HTML 上的维基百科条目说明如下:

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.

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

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

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

回答by cletus

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

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

From C.16. The Named Character Reference ':

C.16。命名字符参考 ':

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.

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

回答by Anon.

'is not part of the HTML 4 standard.

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

"is, though, so is fine to use.

"是,不过,所以很好用。

回答by Zaz

"is valid in both HTML5 and HTML4.

"在 HTML5 和 HTML4 中都有效。

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

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

回答by R. Schreurs

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

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

single quotesand apostrophesare not the same, semantically, although they might look the same.

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

Here'sone apostrophe.

这是一个撇号。

Use ’to insert it if you need HTML4 support.

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

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>

<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:

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

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

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

一篇关于语义正确标记的旧但似乎仍然相关的文章:EM'n EN(和其他阴暗字符)的麻烦

回答by Jeff

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

如果你真的需要单引号,撇号,你可以使用

html    | numeric | hex
&lsquo; | &#145;  | &#x91; // for the left/beginning single-quote and
&rsquo; | &#146;  | &#x92; // for the right/ending single-quote

回答by Sabit Rakhim

In my case it was error when I tried to use quote in text Time's up!. There is was warning from Eslint.

在我的情况下,当我尝试在 text 中使用 quote 时出错Time's up!。有来自 Eslint 的警告。

To fix it I replaced quote with Time&apos;s up!. Result is as expected

为了修复它,我用Time&apos;s up!. 结果如预期