HTML 最佳实践:我应该使用 ’ 或特殊的键盘快捷键?

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

HTML Best Practices: Should I use ’ or the special keyboard shortcut?

htmlapostrophesmart-quotes

提问by user2238214

I know that ’will produce an apostrophe in an HTML document.

我知道这’会在 HTML 文档中产生一个撇号。

I also know that option shift right bracketon a Mac will simply produce a 'character.

我也知道option shift right bracket在 Mac 上只会产生一个'角色。

Are there best practices for writing code, e.g., should I write

是否有编写代码的最佳实践,例如,我应该编写

<b>The User&rsquo;s Forum</b>

or

或者

<b>The User's Forum</b>

(note that by using the keyboard shortcut I've been able to type 'instead of 'above)

(请注意,通过使用键盘快捷键,我可以输入'而不是'上面的内容)

It strikes me that the latter (using the keyboard shortcut) is more robust, as it's not likely to display the raw HTML if, for example, it's not escaped.

我觉得后者(使用键盘快捷键)更健壮,因为它不太可能显示原始 HTML,例如,如果它没有转义。

On the other hand, the special ' character may not be readable in some browsers, perhaps(?).

另一方面,特殊的 ' 字符在某些浏览器中可能不可读,也许(?)。

Anyone have any best practices on this?

任何人都有这方面的最佳做法?

采纳答案by Explosion Pills

I don't think that one is better than the other in general; it depends on how you intend to use it.

总的来说,我不认为一个比另一个更好。这取决于您打算如何使用它。

  • If you want to store it in a DB column that has a charset/collation that does not support the right single quote character, you may run into storing it as the multi-byte character instead of 7-bit ASCII (&rsquo;).
  • If you are displaying it on an html element that specifies a charset that does not support it, it may not display in either case.
  • If many developers are going to be editing/viewing this file with editors/keyboards that do not support properly typing or displaying the character, you may want to use the entity
  • If you need to convert the file between various character encodings or formats, you may want to use the entity
  • If your HTML code may escape entities improperly, you may want to use the character.
  • 如果要将其存储在具有不支持正确单引号字符的字符集/排序规则的 DB 列中,您可能会遇到将其存储为多字节字符而不是 7 位 ASCII ( &rsquo;) 的情况。
  • 如果您在指定不支持它的字符集的 html 元素上显示它,则在任何一种情况下它都可能不会显示。
  • 如果许多开发人员要使用不支持正确键入或显示字符的编辑器/键盘编辑/查看此文件,您可能需要使用实体
  • 如果您需要在各种字符编码或格式之间转换文件,您可能需要使用实体
  • 如果您的 HTML 代码可能不正确地转义实体,您可能需要使用该字符。

In general I would lean more towards using the character because as you point out it is easier to read and type.

一般来说,我更倾向于使用这个角色,因为正如你所指出的那样,它更容易阅读和打字。

回答by jsring

Typographically, the correct glyph to use in sentence punctuation is the quote mark, both single (including for apostrophes) and double quotes. The straight-looking mark that we often see on the web is called a prime, which also comes in single and double varieties and has limited uses, mostly for measurements.

在排版上,在句子标点符号中使用的正确字形是引号,包括单引号(包括撇号)和双引号。我们在网络上经常看到的直视标记称为质数,它也有单双两种,用途有限,主要用于测量。

This articleexplains how to use them correctly.

本文解释了如何正确使用它们。

回答by Manuel Gutierrez Rojas

With &rsquo;you know for certain that the output will be correct, no matter what.

随着&rsquo;你肯定知道的输出将是正确的,不管是什么。

I wish &apos;would output the proper apostrophe and not the typewriter apostrophe. It's 2013, for Christ' sake!

我希望&apos;输出正确的撇号而不是打字机的撇号。看在上帝的份上,现在是 2013 年!

回答by Lori123

You should only use &rsquo;if your intention is to make either a closed single quotation mark or an apostrophe. Both of these punctuation marks are curved in shape in most fonts. If your intent is to make a foot mark, go the other route. A foot mark is always a straight vertical mark.

&rsquo;当您打算使用单引号或撇号时才应使用。这两个标点符号在大多数字体中都是弯曲的。如果您的目的是制作脚印,请走另一条路线。脚印始终是笔直的垂直标记。

It's a matter of typography. One way is correct; the other is not.

这是排版的问题。一种方法是正确的;另一个不是。

回答by Gabe

If your text will be consumed by non-browsers then it's safer to type the character with the keyboard-combo option shift right bracketbecause &rsquo;will not be transformed into an apostrophe by a regular XML or JSON parser. (e.g. if you are serving this content to native Android/iOS apps).

如果您的文本将被非浏览器使用,那么使用键盘组合键入字符会更安全,option shift right bracket因为&rsquo;常规 XML 或 JSON 解析器不会将其转换为撇号。(例如,如果您将此内容提供给本机 Android/iOS 应用程序)。

回答by Drew Noakes

One risk of using the keyboard shortcut is that it requires using a non-ASCII encoding. That might be fine, but if your source is loaded by different editors in different locales, you might hit trouble somewhere along the line.

使用键盘快捷键的一个风险是它需要使用非 ASCII 编码。这可能没问题,但是如果您的源代码是由不同语言环境的不同编辑器加载的,那么您可能会在某处遇到麻烦。

It might be safer to use either &#8217;or &rsquo;(which are equivalent) as both are ASCII.

使用&#8217;or &rsquo;(等效)可能更安全,因为两者都是 ASCII。