为什么是自动关闭的iframe标签阻止进一步显示DOM元素? [英] Why is a self-closing iframe tag preventing further DOM elements to be displayed?
问题描述
在Firefox和Safari上,以下代码仅显示第一个iframe
On Firefox and Safari, the following code displays only the first iframe
<iframe src="http://www.bing.com"/>
<iframe src="http://www.tsr.ch"/>
而添加结束标签解决问题
whereas adding the closing tag solves the issue
<iframe src="http://www.bing.com"></iframe>
<iframe src="http://www.tsr.ch"></iframe>
我不明白为什么它不起作用。当使用DOMParser解析第二个例子时,它反过来会转换为自动关闭iframe。
I don't understand why it does not work. When parsing the second example with DOMParser, it anyways does the transformation to self-closing iframes.
fiddle here: http://jsfiddle.net/hLcukz6p/
fiddle here : http://jsfiddle.net/hLcukz6p/
推荐答案
因为 iframe
元素不是一个自我关闭元素。您正在使用的Firefox和Safari的版本将最终将 />
视为>
和假设它之后的所有东西都包含在 iframe
中。
Because the iframe
element isn't a self-closing element. The versions of Firefox and Safari you're using are treating the />
at the end as just >
and assuming everything after it is contained within the iframe
.
如果我们尝试传递你通过的代码 W3C的验证器,我们会看到以下错误:
If we attempt to pass the code you've given through W3C's validator we'll see the following errors:
错误:在非空白HTML元素上使用的自动关闭语法(
/>
)。忽略斜杠并将其视为起始标签。
Error: Self-closing syntax (
/>
) used on a non-void HTML element. Ignoring the slash and treating as a start tag.
<iframe src="http://www.bing.com"/>
错误:期望文本或结束标签时看到的文件结束。
Error: End of file seen when expecting text or an end tag.
</html>
错误:未关闭元素 iframe
p>
Error: Unclosed element iframe
.
<iframe src="http://www.bing.com"/>
如果您使用浏览器的Element Inspector检查文档,看看发生了什么。
If you inspect your document with your browser's Element Inspector, you'll see what's going on.
我使用的Chrome转换无效的< iframe ... />
to < iframe ...>< / iframe>
:
Chrome, which I'm using, converts the invalid <iframe ... />
to <iframe ...></iframe>
:
这篇关于为什么是自动关闭的iframe标签阻止进一步显示DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!