Html 单引号 vs 双引号 (' vs ")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2373074/
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
Single vs Double quotes (' vs ")
提问by ctrlShiftBryan
I've always used single quotes when writing my HTML by hand. I work with a lot of rendered HTML which always uses double quotes. This allows me to determine if the HTML was written by hand or generated. Is this a good idea?
在手动编写 HTML 时,我总是使用单引号。我使用了很多渲染的 HTML,它们总是使用双引号。这使我能够确定 HTML 是手工编写还是生成的。这是一个好主意吗?
What is the difference between the two? I know they both work and are supported by all modern browsers but is there a real difference where one is actually better than the other in different situations?
两者有什么区别?我知道它们都可以工作并且得到所有现代浏览器的支持,但是在不同情况下一个实际上比另一个更好的真正区别是否存在?
回答by Aito
The w3 org said:
w3 组织说:
By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa. Authors may also use numeric character references to represent double quotes (
"
) and single quotes ('
). For double quotes authors can also use the character entity reference"
.
默认情况下,SGML 要求使用双引号(ASCII 十进制 34)或单引号(ASCII 十进制 39)分隔所有属性值。当值由双引号分隔时,单引号可以包含在属性值中,反之亦然。作者还可以使用数字字符引用来表示双引号 (
"
) 和单引号 ('
)。对于双引号,作者还可以使用字符实体引用"
。
So... seems to be no difference. Only depends on your style.
所以……好像没什么区别。只取决于你的风格。
回答by NibblyPig
I use "
as a top-tier and '
as a second tier, as I imagine most people do. For example
我将其"
用作顶级和'
二级,正如我想象的大多数人所做的那样。例如
<a href="#" onclick="alert('Clicked!');">Click Me!</a>
In that example, you must use both, it is unavoidable.
在那个例子中,你必须同时使用两者,这是不可避免的。
回答by Dustin Poissant
Quoting Conventions for Web Developers
Web 开发人员的引用约定
The Short Answer
简短的回答
In HTML the use of single quotes (') and double quotes (") are interchangeable, there is no difference.
在 HTML 中,单引号 (') 和双引号 (") 的使用可以互换,没有区别。
But consistency is recommended, therefore we must pick a syntax convention and use it regularly.
但是建议保持一致性,因此我们必须选择一个语法约定并定期使用它。
The Long Answer
长答案
Web Developmentoften consists of many programming languages. HTML, JS, CSS, PHP, ASP, RoR, Python, ect. Because of this we have many syntax conventions for different programing languages. Often habbits from one language will follow us to other languages, even if it is not considered "proper" i.e. commenting conventions. Quoting conventions also falls into this category for me.
Web 开发通常由多种编程语言组成。HTML、JS、CSS、PHP、ASP、RoR、Python 等。因此,对于不同的编程语言,我们有许多语法约定。通常来自一种语言的习惯会跟随我们到其他语言,即使它不被认为是“正确的”,即注释约定。引用约定对我来说也属于这一类。
But I tend to use HTML tightly in conjunction with PHP. And in PHP there is a majordifference between single quotes and double quotes. In PHP with double quotes "you can insert variables directly within the text of the string". (scriptingok.com) And when using single quotes "the text appears as it is". (scriptingok.com)
但我倾向于将 HTML 与 PHP 紧密结合使用。在 PHP 中,单引号和双引号之间存在重大差异。在带有双引号的 PHP 中,“您可以直接在字符串的文本中插入变量”。(scriptingok.com) 当使用单引号时,“文本按原样显示”。(scriptingok.com)
PHP takes longer to process double quoted strings. Since the PHP parser has to read the whole string in advance to detect any variable inside—and concatenate it—it takes longer to process than a single quoted string. (scriptingok.com)
PHP 需要更长的时间来处理双引号字符串。由于 PHP 解析器必须提前读取整个字符串以检测其中的任何变量(并将其连接起来),因此处理时间比单引号字符串更长。(scriptingok.com)
Single quotes are easier on the server. Since PHP does not need to read the whole string in advance, the server can work faster and happier. (scriptingok.com)
服务器上的单引号更容易。由于 PHP 不需要预先读取整个字符串,因此服务器可以更快更快乐地工作。(scriptingok.com)
Other things to consider
其他需要考虑的事项
- Frequency of double quotes within string. I find that I need to use double quotes (") within my strings more often than I need to use single quotes (') within strings. To reduce the number of character escapes needed I favor single quote delimiters.
- It's easier to make a single quote. This is fairly self explanatory but to clarify, why press the SHIFT key more times then you have to.
- 字符串中双引号的频率。我发现我需要在字符串中使用双引号 (") 比在字符串中使用单引号 (') 的频率更高。为了减少所需的字符转义次数,我倾向于使用单引号分隔符。
- 制作单引号更容易。这是相当不言自明的,但要澄清一下,为什么要按更多次 SHIFT 键,那么您必须这样做。
My Convention
我的约定
With this understanding of PHP I have set the convention (for myself and the rest of my company) that strings are to be represented as single quotes by default for server optimization. Double quotes are used within the string if a quotes are required such as JavaScript within an attribute, for example:
有了对 PHP 的这种理解,我已经设置了约定(为我自己和我公司的其他人),默认情况下,字符串将被表示为单引号以进行服务器优化。如果需要引号(例如属性中的 JavaScript),则在字符串中使用双引号,例如:
<button onClick='func("param");'>Press Me</button>
<button onClick='func("param");'>Press Me</button>
Of course if we are in PHP and want the parser to handle PHP variables within the string we should intentionally use double quotes. $a='Awesome'; $b = "Not $a";
当然,如果我们使用 PHP 并且希望解析器处理字符串中的 PHP 变量,我们应该有意使用双引号。 $a='Awesome'; $b = "Not $a";
Sources
来源
Single quotes vs Double quotes in PHP. (n.d.). Retrieved November 26, 2014, from http://www.scriptingok.com/tutorial/Single-quotes-vs-double-quotes-in-PHP
PHP 中的单引号与双引号。(nd)。2014 年 11 月 26 日检索,来自http://www.scriptingok.com/tutorial/Single-quotes-vs-double-quotes-in-PHP
回答by stko
If it's all the same, perhaps using single-quotes is better since it doesn't require holding down the shift key. Fewer keystrokes == less chance of RSI.
如果都一样,也许使用单引号更好,因为它不需要按住 shift 键。更少的击键 == 更少的 RSI 机会。
回答by David says reinstate Monica
In HTML I don't believe it matters whether you use "
or '
, but it should be used consistently throughout the document.
在 HTML 中,我认为是否使用"
或并不重要'
,但应该在整个文档中始终如一地使用它。
My own usage prefers that attributes/html use "
, whereas all javascript uses '
instead.
我自己的用法更喜欢使用 attributes/html "
,而所有 javascript 都使用'
。
This makes it slightly easier, for me, to read and check. If your use makes more sense for you than mine would, there's no need for change. But, to me, your code would feelmessy. It's personal is all.
这让我更容易阅读和检查。如果您的使用对您来说比我的更有意义,则无需更改。但是,对我来说,你的代码会感觉很混乱。这是个人的一切。
回答by Stuart
Actually, the best way is the way Google recommends. Double quotes: https://google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Quotation_Marks#HTML_Quotation_Marks
实际上,最好的方法是 Google 推荐的方法。双引号:https: //google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Quotation_Marks#HTML_Quotation_Marks
See https://google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Validity#HTML_ValidityQuoted Advice from Google: "Using valid HTML is a measurable baseline quality attribute that contributes to learning about technical requirements and constraints, and that ensures proper HTML usage."
请参阅https://google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Validity#HTML_Validity引用来自 Google 的建议:“使用有效的 HTML 是一个可衡量的基线质量属性,有助于了解技术要求和约束,并确保正确使用 HTML。”
回答by Alan Dong
Using double quotes for HTML
对 HTML 使用双引号
i.e.
IE
<div class="colorFont"></div>
Using single quotes for JavaScript
对 JavaScript 使用单引号
i.e.
IE
$('#container').addClass('colorFont');
$('<div class="colorFont2></div>');
回答by Randy Tang
I know LOTSof people wouldn't agree, but this is what I do and I really enjoy such a coding style: I actually don't use any quote in HTML unless it is absolutely necessary.
我知道很多人不会同意,但这就是我所做的,我真的很喜欢这样的编码风格:我实际上不会在 HTML 中使用任何引号,除非绝对必要。
Example:
例子:
<form method=post action=#>
<fieldset>
<legend>Register here: </legend>
<label for=account>Account: </label>
<input id=account type=text name=account required><br>
<label for=password>Password: </label>
<input id=password type=password name=password required><br>
...
Double quotes are used only when there are spaces in the attribute values or whatever:
仅当属性值或其他内容中有空格时才使用双引号:
<form class="val1 val2 val3" method=post action=#>
...
</form>
回答by MikeP
I have had an issue using Bootstrap where using double quotes did matter vs using single quote (which didn't work). class='row-fluid' gave me issues causing the last span to fall below the other spans rather than sitting nicely beside on the far right, whereas class="row-fluid" worked.
我在使用 Bootstrap 时遇到了一个问题,其中使用双引号与使用单引号(这不起作用)相比很重要。class='row-fluid' 给了我一些问题,导致最后一个跨度低于其他跨度,而不是坐在最右边,而 class="row-fluid" 工作。
回答by Paul Taylor
It makes no difference to the html but if you are generating html dynamically with another programming language then one way may be easier than another.
它与 html 没有区别,但如果您使用另一种编程语言动态生成 html,那么一种方法可能比另一种更容易。
For example in Javathe double quote is used to indicate the start and end of a String, so if you want to include a doublequote within the String you have to escape it with a backslash.
例如,在Java 中,双引号用于指示字符串的开始和结束,因此如果您想在字符串中包含双引号,则必须使用反斜杠对其进行转义。
String s = "<a href=\"link\">a Link</a>"
You don't have such a problem with the single quote, therefore use of the single quote makes for more readable code in Java.
单引号没有这样的问题,因此使用单引号可以使 Java 中的代码更具可读性。
String s = "<a href='link'>a Link</a>"
Especially if you have to write html elements with many attributes.(Note I usually use a library such as jhtml to write html in Java, but not always practical to do so)
特别是如果您必须编写具有许多属性的 html 元素。(注意我通常使用诸如 jhtml 之类的库在 Java 中编写 html,但这样做并不总是实用)