1

我在标签中渲染这行代码以<pre>在屏幕上显示为普通文本:

<img src="http://maps.googleapis.com/maps/api/staticmap?zoom=7&size=700x540&sensor=false$center=London, England&markers=London, England|Leicester, England|">

但是它在多个浏览器中被错误呈现为:

<img src="http://maps.googleapis.com/maps/api/staticmap?zoom=7&size=700x540&sensor=false¢er=London, England&markers=London, England|Leicester, England|">

由于美分符号的转义码是&cent;而不是&cent,我不明白为什么会发生这种情况,而且似乎没有任何方法可以防止它。任何人都可以帮忙吗?

另外,我的文档类型:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

如果有任何区别,这行代码将通过 Javascript 的 innerHTML 方法添加到文档中。

4

1 回答 1

1

仅仅因为某些东西在 a<pre>中并不意味着您不必逃避它(doctype 在这里没有区别)。如果您想在屏幕上显示,任何实例都&应该使用实体。有时你可以不使用它而侥幸逃脱,但这只是浏览器的宽容——你不应该利用它。如果浏览器识别出一个实体,它可能会尝试转换为一个实体,即使分号丢失(再次原谅)。&amp;&

整个事情(来源)应该是:

<pre>&lt;img src=&quot;http://maps.googleapis.com/maps/api/staticmap?zoom=7&amp;size=700x540&amp;sensor=false&amp;center=London, England&amp;markers=London, England|Leicester, England|&quot;&gt;</pre>

顺便说一句,属性值也是如此,例如href. 人们“懒惰”并且不逃避查询字符串中的&符号是很常见的,但您确实应该这样做。

于 2012-03-28T13:42:05.520 回答