Html & 和 & 有什么区别 在 HTML5 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15776556/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:08:30  来源:igfitidea点击:

Whats the difference between & and & in HTML5?

htmlsharehrefmailtosubject

提问by Bhimbim

What is the difference between &and &?

&和 和有&什么区别?

Like in the code bellow, are both working in the same way?

就像下面的代码一样,两者的工作方式都一样吗?

<a href="mailto:EMAIL?subject=BLABLABLA&body=http://URL, SHORT DESCRIPTION"></a>
<a href="mailto:EMAIL?subject=BLABLABLA&amp;body=http://URL, SHORT DESCRIPTION"></a>

回答by Explosion Pills

&amp;is the html special character for &, which has a special meaning as the sigial for html special characters. The fact that href="&"works is a convenience granted by most browsers who are nice enough to properly deal with invalid HTML, but it is technically incorrect. You want to use &amp;because the entity will be converted into &in the HTML code.

&amp;是 的 html 特殊字符&,作为 html 特殊字符的信号具有特殊意义。有效的事实href="&"是大多数浏览器提供的便利,它们足以正确处理无效的 HTML,但在技术上是不正确的。你要使用&amp;是因为实体会被转换成&HTML 代码。

Consider if you used "instead of &. That would end the hrefvalue and make the HTML incorrect, right? You want to use &quot;.

考虑您是否使用了"代替&. 这将结束该href值并使 HTML 不正确,对吗?您想使用&quot;.

See: http://www.w3.org/html/wg/drafts/html/master/single-page.html#syntax-errors"Errors involving fragile syntax constructs" -- in fact it points out that using &is okay if it's not followed by a named character reference, but there are many of those; ideally you would be escaping attribute input more or less automatically.

参见:http: //www.w3.org/html/wg/drafts/html/master/single-page.html#syntax-errors“涉及脆弱语法结构的错误”——事实上它指出使用&是可以的,如果它后面没有一个命名的字符引用,但有很多;理想情况下,您或多或少会自动转义属性输入。

回答by Chuck

In HTML5, they are equivalent in that example. Traditionally, in HTML, only &amp;was correct — but as with so many things, web developers blithely ignored this inconvenient rule and wrote bare ampersands everywhere. For their part, browsers just "did the right thing" and interpreted these ampersands as ampersands. HTML5 standardized this behavior, so now &is allowed by itself as long as what goes afterward does not look like an entity reference.

在 HTML5 中,它们在该示例中是等效的。传统上,在 HTML 中, only&amp;是正确的 - 但与许多事情一样,Web 开发人员轻松地忽略了这个不方便的规则,到处写着裸露的&符号。就他们而言,浏览器只是“做了正确的事情”并将这些&符号解释为&符号。HTML5 对此行为进行了标准化,因此&只要后面的内容看起来不像实体引用,现在就可以单独使用。

回答by helion3

&amp;is the html entity (encoded form) for &, used to describe an ampersand in languages where an ampersand actually means something, like XML.

&amp;是 的 html 实体(编码形式)&,用于描述语言中的&符号,其中&符号实际上意味着某种东西,例如 XML。

HTML rendering tools, like the browser, will see &amp;in the source but render &

HTML 渲染工具,如浏览器,会&amp;在源代码中看到但渲染&