`—` 或 `—` 在 HTML 输出上有什么区别吗?

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

`—` or `—` is there any difference in HTML output?

htmlascii

提问by Adam Ramadhan

—or —

—或者 —

Is there a difference between these? Is one better-supported than the other?

这些有区别吗?一个比另一个得到更好的支持吗?

回答by Quentin

SGML parsers (or XML parsers in the case of XHTML) can handle —without having to process the DTD (which doesn't matter to browsers as they just slurp tag soup), while —is easier for humans to read and write in the source code.

SGML 解析器(或 XHTML 中的 XML 解析器)—无需处理 DTD(这对浏览器无关紧要,因为它们只是吸食标签汤)即可处理,同时—更易于人类在源代码中读写。

Personally, I would stick to a literal em-dash and ensure that my character encoding settings were consistent.

就个人而言,我会坚持使用文字长破折号并确保我的字符编码设置一致。

回答by RoToRa

They are exactly the same character. See: http://en.wikipedia.org/wiki/Dash

他们是完全一样的角色。请参阅:http: //en.wikipedia.org/wiki/Dash

Barring browser bugs they will display the same in all cases, so the only difference would be concerning code readability, which would point to —.

除了浏览器错误,它们在所有情况下都会显示相同的内容,因此唯一的区别在于代码可读性,这将指向—.

Or, if you are using UTF-8 as a charset in your HTML document, you could enter the character directly. That would also display exactly the same.

或者,如果您在 HTML 文档中使用 UTF-8 作为字符集,则可以直接输入字符。这也将显示完全相同。

回答by wrlee

—:: —:: \u2014

—:: —::\u2014

When representing the m-dash in a JavaScript text string for output to HTML, note that it will be represented by its unicode value. There are cases when ampersand characters ('&') will not be resolved—notably certain contexts within JSX. In this case, neither —nor —will work. Instead you need to use the Unicode escape sequence: \u2014.

当在 JavaScript 文本字符串中表示 m-dash 以输出到 HTML 时,请注意它将由其 unicode 值表示。在某些情况下,与号字符 ('&') 不会被解析——特别是 JSX 中的某些上下文。在这种情况下,既—不会也—不会工作。相反,您需要使用 Unicode 转义序列:\u2014

For example, when implementing a render()method to output text from a JavaScript variable:

例如,在实现render()从 JavaScript 变量输出文本的方法时:

render() {
   let text='JSX transcoders will preserve the & character—to ' 
            + 'protect from possible script hacking and cross-site hacks.'
   return (
     <div>{text}</div>
   )
}

This will output:

这将输出:

<div>JSX transcoders will preserve the & character&mdash;to protect from possible script hacking and cross-site hacks.</div>

Instead of the &– prefixed representation, you should use \u2014:

&您应该使用 \u2014而不是– 前缀表示:

let text='JSX transcoders will preserve the & character\u2014to …'

回答by Kamran

From W3 web site Common HTML entities used for typography

来自 W3 网站用于排版的常用 HTML 实体

For the sake of portability, Unicode entity references should be reserved for use in documents certain to be written in the UTF-8 or UTF-16 character sets. In all other cases, the alphanumeric references should be used.

出于可移植性的考虑,Unicode 实体引用应保留用于特定以 UTF-8 或 UTF-16 字符集编写的文档中。在所有其他情况下,应使用字母数字参考。

Translation: If you are looking for widest support, go with &mdash;

翻译:如果您正在寻找最广泛的支持,请选择 &mdash;

回答by Joachim VR

Could be that using the numeral code is more universal, as it's a direct reference to a character in the html entity table, but I guess they both work everywhere. The first notation is just massively easier to remember for a lot of characters.

可能是使用数字代码更通用,因为它是对 html 实体表中字符的直接引用,但我想它们都适用于任何地方。对于许多字符来说,第一个符号更容易记住。