HTML 中的右尖括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10462348/
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 00:10:53  来源:igfitidea点击:

Right angle bracket in HTML

html

提问by rwallace

For obvious reasons, the left angle bracket cannot be used literally in HTML text. The status of the right angle bracket is not quite so clear. It does work when I try it, but of course browsers are notoriously forgiving of constructs that are not strictly valid HTML.

出于显而易见的原因,左尖括号不能在 HTML 文本中按字面意思使用。右尖括号的状态不是很清楚。当我尝试它时它确实有效,但是当然,众所周知,浏览器对不是严格有效的 HTML 的构造是非常宽容的。

http://www.w3.org/TR/html4/charset.html#h-5.4seems to be saying it's valid, though may not be supported by older browsers, but also makes specific mention of quoted attribute values. Is it necessary to html encode right angle brackets?also says it's valid but again specifically talks about quoted attribute values.

http://www.w3.org/TR/html4/charset.html#h-5.4似乎在说它是有效的,虽然旧浏览器可能不支持,但也特别提到了引用的属性值。是否需要对直角括号进行 html 编码?还说它是有效的,但再次特别谈到引用的属性值。

What's the answer for plain chunks of text (contents of a <pre>Element happens to be the case I'm looking at), and does it differ in any way?

纯文本块的答案是什么(<pre>元素的内容恰好是我正在查看的情况),它是否有任何不同?

回答by Jukka K. Korpela

The character “>” can be used as such as data character in any version of HTML, both in element content and in an attribute value. This follows from the lack of any statement to the contrary in the specifications.

字符“>”可以用作任何版本的 HTML 中的数据字符,包括元素内容和属性值。这是由于规范中没有任何相反的陈述。

It is often routinely escaped as &gt;, which is valid but not required for any formal or technical reason. It is used partly because people assume it is needed the same way as the “<” character needs to be escaped, partly for symmetry: writing, say, &lt;code&gt;may look more symmetric than &lt;code>.

它通常经常被转义为&gt;,这是有效的,但不是出于任何正式或技术原因所必需的。使用它的部分原因是人们认为需要它的方式与需要转义“<”字符的方式相同,部分原因是为了对称:例如,书写&lt;code&gt;可能看起来比&lt;code>.

The character “>” is the GREATER THAN character. It is used in many contexts, like HTML markup, as a delimiter of a kind, in a bracket-like manner, but the real angle brackets, as used in some mathematical notations, are rather different, such as “?” U+27E9. If you need to include angle brackets in an HTML document, you have some serious issues to consider, but they relate to fonts (and semantics), not to any potential clash with markup-significant characters.

字符“>”是大于字符。它在许多上下文中使用,如 HTML 标记,作为一种分隔符,以类似括号的方式,但真正的尖括号,如在某些数学符号中使用的,是相当不同的,例如“?” U+27E9。如果您需要在 HTML 文档中包含尖括号,您需要考虑一些严重的问题,但它们与字体(和语义)有关,而不是与标记重要字符的任何潜在冲突。

回答by Oded

Right angle brackets are legal within a <pre>tag or as text within an element.

右尖括号在<pre>标签内或作为元素内的文本是合法的。

There is no ambiguity when using them in this manner and parsers have no issue with "understanding" them.

以这种方式使用它们时没有歧义,解析器在“理解”它们时没有问题。

Personally, I just escape these whenever I need to use them, just to match left angle brackets...

就个人而言,我只是在需要使用它们时转义这些,只是为了匹配左尖括号......