对 HTML 属性使用单引号是否正确?

发布于 2024-07-08 04:21:27 字数 294 浏览 16 评论 0 原文

最近我看到了很多这样的内容:

<a href='http://widget-site-example.com/example.html'>
    <img src='http://widget-site-example.com/ross.jpg' alt='Ross&#39;s Widget' />
</a>

Is it valid to use singlequotes in HTML? 正如我在上面强调的那样,这也是有问题的,因为你必须转义撇号。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

も让我眼熟你 2024-07-15 04:21:27

使用单引号当然是有效的(HTML 4.01,第 3.2 节.2)。 我还没有注意到这种趋势,但也许有一些框架为您访问过的网站提供支持,而这些网站恰好使用单引号进行引用。

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.

不知所踪 2024-07-15 04:21:27

我发现,当使用使用双引号字符串文字的编程语言动态生成 HTML 时,使用单引号非常方便。

例如

String.Format("<a href='{0}'>{1}</a>", Url, Desc)

I find using single quotes is handy when dynamically generating HTML using a programming language that uses double quote string literals.

e.g.

String.Format("<a href='{0}'>{1}</a>", Url, Desc)
心清如水 2024-07-15 04:21:27

当使用 PHP 生成 HTML 时,执行以下操作可能更容易:

$html = "<img src='$url' />";

比将字符串与变量与字符串连接起来更容易,因为 PHP 解析双引号字符串中的变量。

When using PHP to generate HTML it can be easier to do something like:

$html = "<img src='$url' />";

than concatenating a string with a variable with a string, as PHP parses variables in double-quoted strings.

半衾梦 2024-07-15 04:21:27

您可能想要使用单引号的另一种情况:在数据属性中存储 JSON 数据:

<tr data-info='{"test":true}'></tr>

JSON 对象键必须使用双引号,因此属性本身不能使用双引号。

Another case where you may want to use single quotes: Storing JSON data in data attributes:

<tr data-info='{"test":true}'></tr>

JSON object keys must be in double quotes, so the attribute itself cannot.

勿忘初心 2024-07-15 04:21:27

有人可能会在 PHP 中使用它来避免转义 " 如果他们使用双引号字符串来解析其中的变量,或者避免使用字符串连接运算符。

示例:

echo "<input type='text' value='$data'/>";

而不是

echo "<input type=\"text\" value=\"$data\" />";

or

echo '<input type="text" value="' . $data . '" />';

现在我总是坚持对 HTML 使用双引号和单引号对于 JavaScript。

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.

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.

机场等船 2024-07-15 04:21:27

当您想嵌入双引号时更容易。

It's easier when you want to embed double quotes.

旧城空念 2024-07-15 04:21:27

在 ASP.NET 中,如果您使用 data-,则使用单引号会更容易属性中的绑定表达式

<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />

In ASP.NET, it's easier to use single quotes if you're using data-binding expressions in attributes:

<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />
末蓝 2024-07-15 04:21:27

单引号在 (X)HTML 中是完全合法的。 另一方面,使用反斜杠来转义它们则不然。 Ross\ 是带有 alt 文本“Ross\”的图像,以及空的 sWidget /Widget' 属性。 在 HTML 中转义撇号的正确方法是 '

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

白昼 2024-07-15 04:21:27

在 PHP 中,echo 需要多个参数。 因此,如果想省略连接运算符,他们可以执行类似的操作,但仍然使用双引号:

echo '<input type="text" value="', $data, '" />';

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 :

echo '<input type="text" value="', $data, '" />';
[旋木] 2024-07-15 04:21:27

单引号会生成更干净、更少混乱的页面。 您不应该在 HTML 字符串中转义它们,使用哪个是您的选择...我的偏好通常是单引号,如果我需要在字符串中包含单引号(例如作为字符串内字符串的分隔符),我用双引号表示 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.

悲欢浪云 2024-07-15 04:21:27

如果您希望 有效 然后两个 适用于属性。 HTML4 可以在此处进行验证:https://validator.w3.org/

如果您是 仅支持现代浏览器 ( 及更高版本),那么您可以使用 语法允许单引号、双引号和无引号(只要属性值中没有特殊字符)。 HTML5 可以在此处进行验证:https://validator.w3.org/nu

If you want to the to be valid or then both or will work for attributes. HTML4 can be validated here: https://validator.w3.org/

If you are supporting only modern browsers ( and higher) then you can use the 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

情定在深秋 2024-07-15 04:21:27

什么反对单引号?

你可以在你的 html 代码中使用单/双引号,没有任何问题,只要你在当前标签中保持相同的引用样式(许多浏览器甚至不会抱怨这一点,并且验证只是希望如果你从引用,以相同的结尾,在相同的属性内)

免费支持随机引用样式! (是的;D)

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 )

Free support to random quoting styles!!! (yay ;D )

羁绊已千年 2024-07-15 04:21:27

我知道这是一个旧线程,但仍然非常相关。

如果您想控制生成的 HTML 中的引号,可以使用 sprintf() PHP 中的函数(以及许多其他语言中可用的类似调用):

$html = sprintf('<a href="%s">%s</a>', $url, $text);

使用 sprintf() 允许通过从数据库或配置文件检索格式字符串或通过翻译机制轻松修改格式字符串。

它的可读性非常好,并且允许在生成的 HTML 中使用双引号或单引号,只需很少的更改并且从不进行任何转义:

$html = sprintf("<a href='%s'>%s</a>", $url, $text);

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

It is very readable, and allows either double or single quotes in the generated HTML, with very little change and never any escaping:

$html = sprintf("<a href='%s'>%s</a>", $url, $text);
森林很绿却致人迷途 2024-07-15 04:21:27

为什么不按 SHIFT 键保存。 相同的里程少敲击一次按键。

Why not save pressing the SHIFT Key. One less keystroke for the same milage.

旧竹 2024-07-15 04:21:27

让我们很好地解决这个问题


  1. 双引号 可以
  2. 单引号 可以
  3. 不加引号 也可以,只要你没有其中之一这些字符:
    • 空间(显然)
    • “<” 或“>” (有道理)
    • 等号(不知道为什么,但没关系)
    • 任何类型的引号:双引号、单引号、反引号(同样,有意义)

有趣的是,即使在今天,有些人仍然坚持“仅双引号”标准,而这实际上从来都不是一个标准,但一个约定。 另外,如果您的语言将撇号作为自然元素(英语,有人吗?),那就更方便了,但在我看来,增加 PHP 字符串组合的复杂性为此付出的代价太高了。

Let's settle this argument for good


  1. Double quotes are OK
  2. Single quotes are OK
  3. No quotes are OK, as long as you don't have one of these characters:
    • space (obviously)
    • "<" or ">" (makes sense)
    • equal sign (not sure why but ok)
    • any kind of quote: double, single, backtick (again, makes sense)

Funny how some people, even today, stick to the "double quote only" standard which was never really a standard but a convention. Also, if your language has apostrophes as a natural element (English, anyone?) it's more convenient, but in my opinion, increasing the complexity of PHP string compositions is way too high a price to pay for this.

月下伊人醉 2024-07-15 04:21:27

您应该完全避免引号。

在您的示例中,只有一个带引号的属性实际上需要引号。

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

如果您确实使用引号,则没有硬性规定,但我见过最常见的单引号,必要时在内部使用双引号。

使用双引号不会通过某些验证器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文