在手写HTML时,我总是使用单引号。我使用了很多渲染的HTML,它们总是使用双引号。这使我能够确定HTML是手工编写的还是生成的。这是个好主意吗?

这两者之间有什么区别?我知道它们都可以工作,而且所有现代浏览器都支持它们,但在不同的情况下,其中一种真的比另一种更好吗?


当前回答

我知道很多人不会同意,但这就是我所做的,我真的很喜欢这样的编码风格:我实际上在HTML中不使用任何引用,除非绝对必要。

例子:

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

双引号只在属性值中有空格时使用:

<form class="val1 val2 val3" method=post action=#>
  ...
</form>

其他回答

在PHP中,使用双引号会导致性能略有下降,因为变量名要计算,所以在实际中,我在编写代码时总是使用单引号:

echo "This will print you the value of $this_variable!";
echo 'This will literally say $this_variable with no evaluation.';

所以你可以这样写;

echo 'This will show ' . $this_variable . '!';

我相信Javascript也有类似的功能,所以在性能上有非常小的改进,如果这对你来说很重要的话。


此外,如果你一直查看HTML规范2.0,这里列出的所有标签;

W3 HTML DTD参考

(使用doublequotes。)无论你更经常使用哪一种,一致性都很重要。

我知道很多人不会同意,但这就是我所做的,我真的很喜欢这样的编码风格:我实际上在HTML中不使用任何引用,除非绝对必要。

例子:

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

双引号只在属性值中有空格时使用:

<form class="val1 val2 val3" method=post action=#>
  ...
</form>

HTML使用双引号

i.e.

<div class="colorFont"></div>

JavaScript中使用单引号

i.e.

$('#container').addClass('colorFont');
$('<div class="colorFont2></div>');

实际上,最好的方法是谷歌推荐的方法。双引号: https://google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Quotation_Marks#HTML_Quotation_Marks

看到https://google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Validity HTML_Validity 引用谷歌的建议:“使用有效的HTML是一个可衡量的基准质量属性,有助于了解技术需求和约束,并确保正确使用HTML。”

用"而不是'当:

<input value="user"/> //Standard html
<input value="user's choice"/> //Need to use single quote
<input onclick="alert('hi')"/> //When giving string as parameter for javascript function

在以下情况下用'代替":

<input value='"User"'/> //Need to use double quote
var html = "<input name='username'/>" //When assigning html content to a javascript variable