我在我的网站的<title>中使用了HTML5和UTF-8的“&”符号。谷歌在其serp上显示与号fine,所有浏览器在其标题中也是如此。
http://validator.w3.org给了我这个:
&没有开始字符引用。(&可能应该被转义为&。)
我真的需要做&
我并不在意我的页面为了验证而验证,但我很好奇人们对这个问题的看法,以及它是否重要以及为什么重要。
我在我的网站的<title>中使用了HTML5和UTF-8的“&”符号。谷歌在其serp上显示与号fine,所有浏览器在其标题中也是如此。
http://validator.w3.org给了我这个:
&没有开始字符引用。(&可能应该被转义为&。)
我真的需要做&
我并不在意我的页面为了验证而验证,但我很好奇人们对这个问题的看法,以及它是否重要以及为什么重要。
当前回答
更新(2020年3月):W3C验证器不再抱怨转义url。
我正在检查为什么图像url需要转义,因此在https://validator.w3.org中尝试了它。这个解释很好。它强调了即使是url也需要转义。[PS:我猜它将无法转义当它被消费,因为url需要&。有人能澄清一下吗?]
<img alt="" src="foo?bar=qut&qux=fop" />
An entity reference was found in the document, but there is no reference by that name defined. Often this is caused by misspelling the reference name, unencoded ampersands, or by leaving off the trailing semicolon (;). The most common cause of this error is unencoded ampersands in URLs as described by the WDG in "Ampersands in URLs". Entity references start with an ampersand (&) and end with a semicolon (;). If you want to use a literal ampersand in your document you must encode it as "&" (even inside URLs!). Be careful to end entity references with a semicolon or your entity reference may get interpreted in connection with the following text. Also keep in mind that named entity references are case-sensitive; &Aelig; and æ are different characters. If this error appears in some markup generated by PHP's session handling code, this article has explanations and solutions to your problem.
其他回答
你能告诉我们你的头衔是什么吗?当我提交时
<!DOCTYPE html>
<html>
<title>Dolce & Gabbana</title>
<body>
<p>Am I allowed loose & mpersands?</p>
</body>
</html>
到http://validator.w3.org/ -明确要求它使用实验性的HTML 5模式-它没有抱怨&s…
HTML5规则不同于HTML4。在HTML5中它不是必需的——除非&号看起来像一个参数名的开头。"©=2"仍然是一个问题,例如,因为©是版权符号。
然而,在我看来,决定编码或不编码取决于下面的文本是更困难的工作。所以最简单的方法就是一直编码。
如果用户将它传递给您,或者它将在URL中结束,您需要转义它。
如果它以静态文本的形式出现在页面上?所有浏览器都能正确地处理这个问题,您不必太担心,因为它可以工作。
几年前,我们收到一份报告,说我们的一个web应用程序在Firefox中不能正确显示。事实证明,该页面包含一个类似于
<div style="..." ... style="...">
当面对重复的样式属性时,Internet Explorer结合了这两种样式,而Firefox只使用其中一种,因此行为不同。我把标签改成了
<div style="...; ..." ...>
果然,它解决了问题!这个故事的寓意是,浏览器对有效HTML的处理比对无效HTML的处理更一致。所以,现在就修改你该死的加价吧!(或者使用HTML Tidy来修复它。)
这取决于分号在&附近结束的可能性,导致它显示完全不同的内容。
例如,当处理来自用户的输入时(例如,如果在标题标签中包含用户提供的论坛帖子的主题),您永远不知道他们可能会在哪里放置随机分号,并且可能会随机显示奇怪的实体。所以在这种情况下一定要逃避。
当然,对于您自己的静态HTML内容,您可以跳过它,但是包含适当的转义太琐碎了,因此没有理由避免它。