Firefox 渲染出错 - 看到一些非常奇怪的东西
我的以下情况真的很奇怪。基本上,当我查看页面的源代码时,一切看起来都很好,但页面看起来完全错误。所以我决定使用 firebug 查看源代码,而 firebug 显示了一个非常不同的故事。但是,如果我刷新页面,页面看起来不错,并且源和萤火虫匹配。
请参阅下面的源代码,但 firefox 显示的内容和 firebug 显示的内容:
查看源代码显示此内容:
<div class="mainpanel">
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Little-Rock">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB001D_0.jpg" alt="Little Rock" /></td></tr></table></div>
<div class="thumbphototitle">Little Rock</div>
</a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB002D_0.jpg" alt="Split Rock" /></td></tr></table></div>
<div class="thumbphototitle">Split Rock</div>
</a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Rock-Pointer">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB003D_0.jpg" alt="Rock Pointer" /></td></tr></table></div>
<div class="thumbphototitle">Rock Pointer</div>
</a>
</div>
但 firebug 显示此内容,并且它在屏幕上呈现为如下所示:
<div class="mainpanel">
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Little-Rock">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB001D_0.jpg" alt="Little Rock" /></td></tr></table></div>
<div class="thumbphototitle">Little Rock</div>
</a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"></a>
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB002D_0.jpg" alt="Split Rock" /></td></tr></table></div>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"> </a>
<div class="thumbphototitle">
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock">Split Rock</a>
</div>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"> </a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Rock-Pointer">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB003D_0.jpg" alt="Rock Pointer" /></td></tr></table></div>
<div class="thumbphototitle">Rock Pointer</div>
</a>
</div>
有问题的 html 是中间的 a 标签,该标签变得疯狂......
任何想法。
干杯 安东尼
I have the following is really weird. Bassically when I view the source of the page everything looks fine but the page looks all wrong. So I decided to take a look at the source using firebug and firebug shows a very different story. But if I refresh the page the page looks fine and the source and firebug match up.
See below for what the source is but what firefox displays and firebug shows:
View source shows this:
<div class="mainpanel">
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Little-Rock">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB001D_0.jpg" alt="Little Rock" /></td></tr></table></div>
<div class="thumbphototitle">Little Rock</div>
</a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB002D_0.jpg" alt="Split Rock" /></td></tr></table></div>
<div class="thumbphototitle">Split Rock</div>
</a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Rock-Pointer">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB003D_0.jpg" alt="Rock Pointer" /></td></tr></table></div>
<div class="thumbphototitle">Rock Pointer</div>
</a>
</div>
But firebug shows this and it renders on the screen as if its like this:
<div class="mainpanel">
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Little-Rock">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB001D_0.jpg" alt="Little Rock" /></td></tr></table></div>
<div class="thumbphototitle">Little Rock</div>
</a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"></a>
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB002D_0.jpg" alt="Split Rock" /></td></tr></table></div>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"> </a>
<div class="thumbphototitle">
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock">Split Rock</a>
</div>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"> </a>
<a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Rock-Pointer">
<div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB003D_0.jpg" alt="Rock Pointer" /></td></tr></table></div>
<div class="thumbphototitle">Rock Pointer</div>
</a>
</div>
The offending html is the middle a tag which goes crazy...
Any ideas.
Cheers
Anthony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人所说,发生这种情况是因为您的标记无效。更深入一点,问题在于,当解析器在其输入中收到
时,可能意味着两件事:
...
,或者。
只有当知道更多(可能更多)字符时才能做出正确的决定;正如您可能已经注意到的那样,解析是逐步进行的——即您可以在页面完全下载之前看到页面的部分内容。
不幸的是,Mozilla 的 HTML 解析器(从 Firefox 3.6 及更早版本开始)在这种情况下是不确定的——生成的 DOM 取决于 HTML 在通过网络时被分割成的部分。
有一个Mozilla bug,其问题与您的问题非常相似。
我为你感到抱歉,我不知道如何实现(也不想尝试实现;)解决你原来的问题,但也许是一个涉及设置
innerHTML
的黑客(以避免解析器非确定性)是否合适?顺便说一句,检查 HTML5 解析算法如何处理您的标记会很有趣,因为这最终将在浏览器中实现。
Like others said, this happens because your markup is invalid. Going a bit deeper, the problem is that when the parser received
<a><div>
in its input, it may mean two things:<a></a><div>...
in the DOM, or<a><div></div></a>
.The correct decision can be made only when more (potentially much more) characters are known; the parsing, as you could have noticed, happens incrementally -- i.e. you can see parts of the page before it's fully downloaded.
Unfortunately, the Mozilla's HTML parser (as of Firefox 3.6 and earlier) is non-deterministic in this case -- the resulting DOM depends on the portions your HTML is split into, while going over network.
There's a Mozilla bug about a problem that looks very similar to yours.
I'm sorry for you, and I don't know how to implement (nor have any desire to try implementing ;) the solution to your original problem, but perhaps a hack involving setting
innerHTML
(to avoid parser non-determinism) is in order?BTW, it would be interesting to check how the HTML5 parsing algorithm says your markup should be treated, since that's what will eventually be implemented in the browsers.
您不应将块元素/标签(如
)包装在内联标签(如
)中。那是自找麻烦。
You should not wrap block elements/tags (like
<div>
) in inline tags (like<a>
). That's asking for trouble.那是因为您的 HTML 无效。行内元素只能包含其他行内元素,不能包含块元素。
浏览器遇到违反此规则的 HTML 时可以执行任何操作来解析页面(包括不显示页面),并且显然 Firefox 对 anything-at-all 的解释与您的不同。
请注意,您可以通过设置
display
css 属性将等内联元素转换为块元素。但我不完全确定这对于具有附加语义的元素(例如
标记)是否合法。当然,您可以将这些 div 转换为内联元素。
That's because your HTML is invalid. Inline elements can only contain other inline elements and cannot contain block elements.
Browsers encountering HTML which breaks this rule is allowed to do anything at all in order to parse the page (including not displaying the page) and apparently firefox's interpretation of anything-at-all is not the same as yours.
Note that you can convert inline elements like
<span>
to a block element by setting it'sdisplay
css property. But I'm not entirely sure if that is legal for an element with additional semantics such as an<a>
tag. Of course, you could convert those divs to inline elements.我不想停止将块元素放入锚点内。它太有用了:
http://html5doctor.com/block-level-links-in-html- 5/
我开发了一种解决方法,似乎可以禁用渐进式渲染并避免此问题。
我使用服务器端嗅探在用户代理中查找“Firefox/3”。如果找到,我将其放在
之后:
在
之前:
在测试中,我发现只需插入一个空
body 标记之后的
很难确定 FF 的真实想法 - 我很好奇这是否可以解决其他人的问题。
我已经习惯在 IE 上做这样的事情了。 FF3.x 正在争夺我最不喜欢的浏览器新奖。
I don't want to stop putting block elements inside anchors. It's just too useful:
http://html5doctor.com/block-level-links-in-html-5/
I developed a workaround which seems to disable progressive rendering and avoid this problem.
I use server-side sniffing to look for "Firefox/3" in the user-agent. If found, I put this right after
<body>
:And this right before
</body>
:In testing, I found that simply inserting an empty
<script>
tag after the body tag avoided the issue I was experiencing. But it feels more correct and safer doing a show/hide.Hard to know for sure what FF is really thinking - I'm curious to know whether this solves the problem for others.
I'm used to doing this sort of thing for IE. FF3.x is vying for my new least-favorite browser award.