Html & 用于什么

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

What is &amp used for

formshtml

提问by blue-sky

Is there any difference in behaviour of below URL.

以下 URL 的行为是否有任何差异。

I don't know why the &is inserted, does it make any difference ?

我不知道为什么&插入,这有什么区别吗?

www.testurl.com/test?param1=test&current=true

versus

相对

www.testurl.com/test?param1=test&current=true

回答by Quentin

&is HTML for "Start of a character reference".

&是“字符引用的开始”的 HTML。

&is the character reference for "An ampersand".

&是“和号”的字符参考。

&current;is not a standard character reference and so is an error (browsers may try to perform error recovery but you should not depend on this).

&current;不是标准字符引用,因此是错误(浏览器可能会尝试执行错误恢复,但您不应依赖于此)。

If you used a character reference for a real character (e.g. ™) then it (™) would appear in the URL instead of the string you wanted.

如果您对真实字符使用了字符引用(例如™),那么它 (™) 将出现在 URL 中,而不是您想要的字符串中。

(Note that depending on the version of HTML you use, you may have to end a character reference with a ;, which is why &trade=will be treated as ™. HTML 4 allows it to be ommited if the next character is a non-word character (such as =) but some browsers (Hello Internet Explorer) have issues with this).

(请注意,根据您使用的 HTML 版本,您可能必须以 结束字符引用;,这就是&trade=将被视为 ™ 的原因。如果下一个字符是非单词字符,HTML 4 允许省略它(例如=),但某些浏览器(您好 Internet Explorer)对此有问题)。

回答by Samjus

HTML doesn't recognize the &but it will recognize &because it is equal to &in HTML

HTML 不能识别,&但它会识别,&因为它等于&HTML

I looked over this post someone had made: http://www.webmasterworld.com/forum21/8851.htm

我查看了某人发表的这篇文章:http: //www.webmasterworld.com/forum21/8851.htm

回答by Jim

My Source: http://htmlhelp.com/tools/validator/problems.html#amp

我的来源:http: //htmlhelp.com/tools/validator/problems.html#amp

Another common error occurs when including a URL which contains an ampersand ("&"):

包含一个包含与号 ("&") 的 URL 时会发生另一个常见错误:

This is invalid:

这是无效的:

a href="foo.cgi?chapter=1&section=2&copy=3&lang=en"

a href="foo.cgi?chapter=1§ion=2©=3&lang=en"

Explanation:

解释:

This example generates an error for "unknown entity section" because the "&"is assumed to begin an entity reference. Browsers often recover safely from this kind of error, but real problems do occur in some cases. In this example, many browsers correctly convert &copy=3 to ?=3, which may cause the link to fail. Since ⟨ is the HTML entity for the left-pointing angle bracket, some browsers also convert &lang=en to 〈=en. And one old browser even finds the entity §, converting &section=2 to §ion=2.

此示例为“未知实体部分”生成错误,因为"&"假定它开始实体引用。浏览器通常会从这种错误中安全地恢复,但在某些情况下确实会出现真正的问题。在这个例子中,许多浏览器正确地将 ©=3 转换为 ?=3,这可能会导致链接失败。由于 ⟨ 是左指尖括号的 HTML 实体,因此一些浏览器还将 &lang=en 转换为 ⟨=en。一个旧浏览器甚至找到实体 §,将 §ion=2 转换为 §ion=2。

So the goal here is to avoid problems when you are trying to validate your website. So you should be replacing your ampersands with &when writing a URL in your markup.

因此,这里的目标是在您尝试验证网站时避免出现问题。因此,&在您的标记中编写 URL 时,您应该将您的 & 号替换为 。

Note that replacing &with &amp; is only done when writing the URL in HTML, where "&"is a special character (along with "<" and ">"). When writing the same URL in a plain text email message or in the location bar of your browser, you would use "&"and not "&amp;". With HTML, the browser translates "&amp;"to "&"so the Web server would only see "&"and not "&amp;"in the query string of the request.

请注意,替换&&amp; 仅在以 HTML 编写 URL 时完成,其中"&"是特殊字符(以及“<”和“>”)。在纯文本电子邮件或浏览器的地址栏中写入相同的 URL 时,您将使用"&"而不是"&amp;"。使用 HTML 时,浏览器会转换"&amp;""&"Web 服务器只会看到"&"而不是"&amp;"在请求的查询字符串中。

Hope this helps : )

希望这可以帮助 : )

回答by Alohci

That a great example. When &currentis parsed into a text nodeit is converted to ¤t. When parsed into an attribute value, it is parsed as &current.

这是一个很好的例子。当&current被解析为文本节点时,它被转换为¤t. 当解析为一个属性值时,它被解析为&current.

If you want &currentin a text node, you should write &amp;currentin your markup.

如果你想要&current一个文本节点,你应该写&amp;current在你的标记中。

The gory details are here: http://dev.w3.org/html5/spec/tokenization.html#consume-a-character-reference

血腥细节在这里:http: //dev.w3.org/html5/spec/tokenization.html#consume-a-character-reference

回答by Urbanek Krzysztof

if you're doing a string of characters. make:

如果你正在做一串字符。制作:

let linkGoogle = 'https://www.google.com/maps/dir/?api=1'; 
let origin = '&origin=' + locations[0][1] + ',' + locations[0][2];


aNav.href = linkGoogle + origin;