Recently I've been seeing a lot of this:
最近我看到很多这样的事情:
<a href='http://widget-site-example.com/example.html'>
<img src='http://widget-site-example.com/ross.jpg' alt='Ross's Widget' />
</a>
Is it valid to use single quotes in HTML? As I've highlighted above it's also problematic because you have to escape apostrophes.
在HTML中使用单引号有效吗?正如我在上面强调的那样,这也是有问题的,因为你必须避免撇号。
15 个解决方案
#1
48
It's certainly valid to use single quotes (HTML 4.01, section 3.2.2). I haven't noticed such a trend, but perhaps there's some framework that powers web sites you've visited that happens to quote using single quotes.
使用单引号当然是有效的(HTML 4.01,第3.2.2节)。我还没有注意到这样的趋势,但也许有一些框架可以让你访问过的网站使用单引号。
#2
44
I find using single quotes is handy when dynamically generating HTML using a programming language that uses double quote string literals.
我发现使用双引号字符串文字的编程语言动态生成HTML时,使用单引号非常方便。
e.g.
如。
String.Format("<a href='{0}'>{1}</a>", Url, Desc)
#3
12
When using PHP to generate HTML it can be easier to do something like:
当使用PHP生成HTML时,可以更容易地执行以下操作:
$html = "<img src='$url' />";
than concatenating a string with a variable with a string, as PHP parses variables in double-quoted strings.
而不是将一个字符串与一个变量与一个字符串连接起来,因为PHP在双引号字符串中解析变量。
#4
4
Someone may use it in PHP to avoid escaping " if they're using double quoted string to parse variables within it, or to avoid using string concatenation operator.
如果使用双引号字符串来解析其中的变量,或者避免使用字符串连接操作符,则可以在PHP中使用它来避免转义。
Example:
例子:
echo "<input type='text' value='$data'/>";
instead of
而不是
echo "<input type=\"text\" value=\"$data\" />";
or
或
echo '<input type="text" value="' . $data . '" />';
Nowadays I always stick to using double quotes for HTML and single quotes for Javascript.
现在我一直坚持使用HTML的双引号和Javascript的单引号。
#5
3
It's easier when you want to embed double quotes.
当你想要嵌入双引号的时候会更容易一些。
#6
3
In ASP.NET, it's easier to use single quotes if you're using data-binding expressions in attributes:
在ASP。NET,如果在属性中使用数据绑定表达式,则更容易使用单引号:
<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />
#7
2
Single quotes are perfectly legal in (X)HTML. Using a backslash to escape them, on the other hand, isn't. <img src='http://widget-site-example.com/ross.jpg' alt='Ross\'s Widget' />
is an image with the alt text "Ross\", and empty s
and Widget
/Widget'
attributes. The correct way of escaping an apostrophe in HTML is '
.
在(X)HTML中,单引号是完全合法的。另一方面,使用反斜杠来转义它们则不是。是一个带有alt文本"Ross\"、空s和Widget'属性的图像。在HTML中避免撇号的正确方法是'
#8
2
In PHP, echo takes multiples parameters. So, if one would like to omit the concatenation operator, they could done something like and still use double quotes :
在PHP中,echo接受多个参数。因此,如果想要省略连接操作符,它们可以做一些类似的事情,并且仍然使用双引号:
echo '<input type="text" value="', $data, '" />';
#9
1
Single quotes generate a cleaner page with less clutter. You shouldn't be escaping them in HTML strings and it's your choice which to use... my preference is single quotes normally and if I need to include a single quote in the string (e.g. as delimiters for a string inside the string), I use double quotes for one & single for the other.
单引号可以生成一个更干净的页面,减少页面的混乱。您不应该将它们转义成HTML字符串,您可以选择使用……我的偏好通常是单引号,如果我需要在字符串中包含一个单引号(例如字符串中字符串的分隔符),那么我就会使用双引号来表示另一个。
#10
1
If you want to the html to be valid html4 or xhtml then both single-quotes or double-quotes will work for attributes. HTML4 can be validated here: https://validator.w3.org/
如果希望html是有效的html4或xhtml,那么单引号或双引号都适用于属性。可以在这里验证HTML4: https://validator.w3.org/
If you are supporting only modern browsers (ie10 and higher) then you can use the html5 syntax which allows single quotes, double quotes, and no quotes (as long as there are no special characters in the attributes' value). HTML5 can be validated here: https://validator.w3.org/nu
如果只支持现代浏览器(ie10或更高版本),那么可以使用html5语法,允许单引号、双引号和不引号(只要属性值中没有特殊字符)。可以在这里验证HTML5: https://validator.w3.org/nu
#11
0
What's against single quotes?
对单引号是什么?
You can have single/double quotes all over your html code without any problem, as long as you keep the same quoting style inside a current tags ( many browser won't complain even about this, and validation just want that if you start with a quote, end with the same, inside the same propriety )
可以有单/双引号都在你的html代码没有任何问题,只要你保持相同的引用风格在当前标签(许多浏览器不会甚至抱怨,和验证只是想,如果你从报价开始,结尾一样,在同样的礼节)
Free support to random quoting styles!!! (yay ;D )
免费支持随机引用样式!!(耶;D)
#12
0
I know this is an old thread, but still very much relevant.
我知道这是一条古老的线索,但仍然非常相关。
If you want control over quotes in your generated HTML, you can use the sprintf() function in PHP (and similar calls available in many other languages):
如果您希望在生成的HTML中控制引号,可以使用PHP中的sprintf()函数(以及许多其他语言中可用的类似调用):
$html = sprintf('<a href="%s">%s</a>', $url, $text);
Using sprintf() allows the format string to be easily modifiable by retrieving it from a database or configuration file, or via translation mechanisms.
使用sprintf()可以通过从数据库或配置文件检索格式字符串或通过转换机制轻松修改格式字符串。
It is very readable, and allows either double or single quotes in the generated HTML, with very little change and never any escaping:
它非常易读,并且在生成的HTML中允许双引号或单引号,几乎没有变化,也不会转义:
$html = sprintf("<a href='%s'>%s</a>", $url, $text);
#13
0
Why not save pressing the SHIFT Key. One less keystroke for the same milage.
为什么不保存按SHIFT键呢?在相同的距离内减少一次按键。
#14
0
Another case where you may want to use single quotes: Storing JSON data in data attributes:
另一个可能需要使用单引号的情况是:在数据属性中存储JSON数据:
<tr data-info='{"test":true}'></tr>
JSON object keys must be in double quotes, so the attribute itself cannot.
JSON对象键必须是双引号,因此属性本身不能。
#15
-4
You should avoid quotes altogether.
In your example only one quoted attribute actually needed quotes.
在您的示例中,只有一个引用的属性实际需要引号。
<!-- best form -->
<a href=http://widget-site-example.com/example.html>
<img src=http://widget-site-example.com/ross.jpg alt='Ross's Widget' />
</a>
If you do use quotes, there is no hard and fast rule, but I've seen most commonly single quotes, with double quotes on the inside if necessary.
如果你用引号,没有硬性的规则,但是我看到过最常见的单引号,如果需要的话,里面有双引号。
Using double quotes won't pass some validators.
使用双引号不会传递一些验证器。
#1
48
It's certainly valid to use single quotes (HTML 4.01, section 3.2.2). I haven't noticed such a trend, but perhaps there's some framework that powers web sites you've visited that happens to quote using single quotes.
使用单引号当然是有效的(HTML 4.01,第3.2.2节)。我还没有注意到这样的趋势,但也许有一些框架可以让你访问过的网站使用单引号。
#2
44
I find using single quotes is handy when dynamically generating HTML using a programming language that uses double quote string literals.
我发现使用双引号字符串文字的编程语言动态生成HTML时,使用单引号非常方便。
e.g.
如。
String.Format("<a href='{0}'>{1}</a>", Url, Desc)
#3
12
When using PHP to generate HTML it can be easier to do something like:
当使用PHP生成HTML时,可以更容易地执行以下操作:
$html = "<img src='$url' />";
than concatenating a string with a variable with a string, as PHP parses variables in double-quoted strings.
而不是将一个字符串与一个变量与一个字符串连接起来,因为PHP在双引号字符串中解析变量。
#4
4
Someone may use it in PHP to avoid escaping " if they're using double quoted string to parse variables within it, or to avoid using string concatenation operator.
如果使用双引号字符串来解析其中的变量,或者避免使用字符串连接操作符,则可以在PHP中使用它来避免转义。
Example:
例子:
echo "<input type='text' value='$data'/>";
instead of
而不是
echo "<input type=\"text\" value=\"$data\" />";
or
或
echo '<input type="text" value="' . $data . '" />';
Nowadays I always stick to using double quotes for HTML and single quotes for Javascript.
现在我一直坚持使用HTML的双引号和Javascript的单引号。
#5
3
It's easier when you want to embed double quotes.
当你想要嵌入双引号的时候会更容易一些。
#6
3
In ASP.NET, it's easier to use single quotes if you're using data-binding expressions in attributes:
在ASP。NET,如果在属性中使用数据绑定表达式,则更容易使用单引号:
<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />
#7
2
Single quotes are perfectly legal in (X)HTML. Using a backslash to escape them, on the other hand, isn't. <img src='http://widget-site-example.com/ross.jpg' alt='Ross\'s Widget' />
is an image with the alt text "Ross\", and empty s
and Widget
/Widget'
attributes. The correct way of escaping an apostrophe in HTML is '
.
在(X)HTML中,单引号是完全合法的。另一方面,使用反斜杠来转义它们则不是。是一个带有alt文本"Ross\"、空s和Widget'属性的图像。在HTML中避免撇号的正确方法是'
#8
2
In PHP, echo takes multiples parameters. So, if one would like to omit the concatenation operator, they could done something like and still use double quotes :
在PHP中,echo接受多个参数。因此,如果想要省略连接操作符,它们可以做一些类似的事情,并且仍然使用双引号:
echo '<input type="text" value="', $data, '" />';
#9
1
Single quotes generate a cleaner page with less clutter. You shouldn't be escaping them in HTML strings and it's your choice which to use... my preference is single quotes normally and if I need to include a single quote in the string (e.g. as delimiters for a string inside the string), I use double quotes for one & single for the other.
单引号可以生成一个更干净的页面,减少页面的混乱。您不应该将它们转义成HTML字符串,您可以选择使用……我的偏好通常是单引号,如果我需要在字符串中包含一个单引号(例如字符串中字符串的分隔符),那么我就会使用双引号来表示另一个。
#10
1
If you want to the html to be valid html4 or xhtml then both single-quotes or double-quotes will work for attributes. HTML4 can be validated here: https://validator.w3.org/
如果希望html是有效的html4或xhtml,那么单引号或双引号都适用于属性。可以在这里验证HTML4: https://validator.w3.org/
If you are supporting only modern browsers (ie10 and higher) then you can use the html5 syntax which allows single quotes, double quotes, and no quotes (as long as there are no special characters in the attributes' value). HTML5 can be validated here: https://validator.w3.org/nu
如果只支持现代浏览器(ie10或更高版本),那么可以使用html5语法,允许单引号、双引号和不引号(只要属性值中没有特殊字符)。可以在这里验证HTML5: https://validator.w3.org/nu
#11
0
What's against single quotes?
对单引号是什么?
You can have single/double quotes all over your html code without any problem, as long as you keep the same quoting style inside a current tags ( many browser won't complain even about this, and validation just want that if you start with a quote, end with the same, inside the same propriety )
可以有单/双引号都在你的html代码没有任何问题,只要你保持相同的引用风格在当前标签(许多浏览器不会甚至抱怨,和验证只是想,如果你从报价开始,结尾一样,在同样的礼节)
Free support to random quoting styles!!! (yay ;D )
免费支持随机引用样式!!(耶;D)
#12
0
I know this is an old thread, but still very much relevant.
我知道这是一条古老的线索,但仍然非常相关。
If you want control over quotes in your generated HTML, you can use the sprintf() function in PHP (and similar calls available in many other languages):
如果您希望在生成的HTML中控制引号,可以使用PHP中的sprintf()函数(以及许多其他语言中可用的类似调用):
$html = sprintf('<a href="%s">%s</a>', $url, $text);
Using sprintf() allows the format string to be easily modifiable by retrieving it from a database or configuration file, or via translation mechanisms.
使用sprintf()可以通过从数据库或配置文件检索格式字符串或通过转换机制轻松修改格式字符串。
It is very readable, and allows either double or single quotes in the generated HTML, with very little change and never any escaping:
它非常易读,并且在生成的HTML中允许双引号或单引号,几乎没有变化,也不会转义:
$html = sprintf("<a href='%s'>%s</a>", $url, $text);
#13
0
Why not save pressing the SHIFT Key. One less keystroke for the same milage.
为什么不保存按SHIFT键呢?在相同的距离内减少一次按键。
#14
0
Another case where you may want to use single quotes: Storing JSON data in data attributes:
另一个可能需要使用单引号的情况是:在数据属性中存储JSON数据:
<tr data-info='{"test":true}'></tr>
JSON object keys must be in double quotes, so the attribute itself cannot.
JSON对象键必须是双引号,因此属性本身不能。
#15
-4
You should avoid quotes altogether.
In your example only one quoted attribute actually needed quotes.
在您的示例中,只有一个引用的属性实际需要引号。
<!-- best form -->
<a href=http://widget-site-example.com/example.html>
<img src=http://widget-site-example.com/ross.jpg alt='Ross's Widget' />
</a>
If you do use quotes, there is no hard and fast rule, but I've seen most commonly single quotes, with double quotes on the inside if necessary.
如果你用引号,没有硬性的规则,但是我看到过最常见的单引号,如果需要的话,里面有双引号。
Using double quotes won't pass some validators.
使用双引号不会传递一些验证器。