简体   繁体   English

&,&,&all在html中表现相似。 怎么样和为什么?

[英]&, &amp, & All behave similarly in html. How and why?

P&G  
P&ampG  
P&G 

All these are same. 所有这些都是一样的。 Is the use of special entities only for javascript/url? 是否仅对javascript / url使用特殊实体?

They do not mean the same thing. 它们并不代表同一件事。

P&G contains an undefined entity reference &G , as a validator would tell you. P&G包含一个未定义的实体引用&G ,正如验证者会告诉你的那样。 What browsers do with such references is that they are treated literally. 浏览器对这些引用的作用是字面上对它们进行处理。 If browsers start recognizing new entities, strange things may happen. 如果浏览器开始识别新实体,可能会发生奇怪的事情。 And they are doing it now, since the added entities as per HTML5 drafts are being added. 他们现在正在这样做,因为正在添加根据HTML5草案添加的实体 Incidentally, &G is not among them. 顺便说一句, &G不在其中。

P&ampG is a quite similar case, except that some browsers with broken parsers might incorrectly take the &amp part as meaning the & character. P&ampG是一个非常类似的情况,除了一些解析器破坏的浏览器可能错误地将&amp部分视为&字符。

P&G is a correct notation that stands for P&G. P&G是一个正确的符号,代表宝洁。

This has nothing to do with JavaScript, and nothing to do with URLs, any more than HTML document content in general. 这与JavaScript无关,与URL无关,一般不包括HTML文档内容。 Ampersands are often needed in URLs, but this does not make the issue a question of URLs. URL中经常需要使用&符号,但这不会使问题成为URL问题。 JavaScript code is often written inside a script element, where special parsing rules may apply, depending on HTML version, but this depends on the content model of the element. JavaScript代码通常写在script元素中,其中可能会应用特殊的解析规则,具体取决于HTML版本,但这取决于元素的内容模型。 (The style element is a similar case.) style元素是类似的情况。)

For best results, you should always use the full form & 为了获得最佳效果,您应该始终使用完整的表格& . Period. 期。

Well, except in environments that have a CDATA content type. 好吧,除了具有CDATA内容类型的环境。 In HTML, that would be elements like <script> , <style> , <xmp> , <listing> and so on. 在HTML中,这将是<script><style><xmp><listing>等元素。 In XHTML, only in explicit CDATA blocks, delimited with <![CDATA[ .. ]]> . 在XHTML中,仅在显式CDATA块中,用<![CDATA[ .. ]]>分隔。

If you do not write the reference out in full, not all browsers will display it the way you want, and you may get into trouble later if you inadvertently write things like "select&copy&paste". 如果您没有完整地编写参考文献,并非所有浏览器都会以您希望的方式显示它,如果您无意中写了“选择并复制和粘贴”之类的内容,您可能会遇到麻烦。

No. Even HTML doesn't understand them and thus they are called Special Characters. 不。甚至HTML也不理解它们,因此它们被称为特殊字符。

However, modern browsers correct that for you. 但是,现代浏览器会为您纠正这一点。 (example of modern browsers correction): (现代浏览器更正的例子):

Create a document with extension .htm|.html and save in the document after typing <h1>hi!</h1> . 创建扩展名为.htm | .html的文档,并在输入<h1>hi!</h1>后保存在文档中。 you will be able to see some heading. 你将能够看到一些标题。 which should be wrong... (back to the topic) 应该是错的......(回到主题)

There are hundreds of these little suckers. 这些小吸盘有数百个。 but main thing is, try to follow the standard as it: 但最重要的是,尽量遵循以下标准:

  1. helps your colleague understand your code better. 帮助您的同事更好地理解您的代码。
  2. just because your browser lets you do this is old excuse. 只是因为你的浏览器让你这样做是旧的借口。
  3. you can fall into trouble without you even remembering how? 你甚至不记得怎么会遇到麻烦?
  4. read about it in Complete Ref or http://www.w3schools.com/tags/ref_entities.asp 在Complete Ref或http://www.w3schools.com/tags/ref_entities.asp中阅读

As a try writing <>10<20<> in browser, it will show you as it is if new modern browser, if old, who knows what would happen? 尝试在浏览器中编写<>10<20<> ,如果新的现代浏览器(如果是旧的话)知道会发生什么,它会显示你的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM