对 HTML 属性使用单引号是否正确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/242766/
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
Is it correct to use single quotes for HTML attributes?
提问by Ross
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 中使用单引号是否有效?正如我在上面强调的那样,它也存在问题,因为您必须避开撇号。
采纳答案by Greg Hewgill
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 节)。我没有注意到这种趋势,但也许有一些框架可以为您访问过的网站提供支持,这些网站恰好使用单引号进行引用。
回答by Ady
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)
回答by Dean Rather
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 解析双引号字符串中的变量。
回答by Imran
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 使用单引号。
回答by Imran
It's easier when you want to embed double quotes.
当您想要嵌入双引号时更容易。
回答by Danko Durbi?
In ASP.NET, it's easier to use single quotes if you're using data-binding expressionsin attributes:
在 ASP.NET 中,如果您在属性中使用数据绑定表达式,则使用单引号会更容易:
<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />
回答by bryc
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 对象键必须在双引号中,因此属性本身不能。
回答by Ms2ger
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 中是完全合法的。另一方面,使用反斜杠来逃避它们不是。是带有替代文本“Ross\”、空和/属性的图像。在 HTML 中转义撇号的正确方法是.<img src='http://widget-site-example.com/ross.jpg' alt='Ross\'s Widget' />
s
Widget
Widget'
'
回答by matv
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, '" />';
回答by Anthony
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 字符串中转义它们,这是你的选择……我的偏好通常是单引号,如果我需要在字符串中包含一个单引号(例如作为字符串中字符串的分隔符),我对一个使用双引号,对另一个使用单引号。