使用JavaScript阅读IFrame内容 [英] Read IFrame content using JavaScript
问题描述
好的,这是我第一次认真对待IFrame,我似乎无法理解一些事情:
Ok, This is my first time dealing seriously with IFrames and I cant seem to understand a few things:
首先我测试的示例代码:
First the sample code I am testing with:
<head>
<script type="text/javascript">
function init(){
console.log("IFrame content: " + window.frames['i1'].document.getElementsByTagName('body')[0].innerHTML);
}
</script>
</head>
<body onload="init();">
<iframe name="i1" src="foo.txt"/>
</body>
文件foo.txt如下所示:
the file "foo.txt" looks like this:
sample text file
问题:
1)iframe似乎表现为HTML文档,而文件文本实际上是身体的一部分。为什么? IFrame是否是HTML文档的规则。 iframe的内容不可能只是纯文本吗?
1) The iframe seems to be behaving as a HTML document and the file text is actually part of the body instead. Why ? Is it a rule for an IFrame to be a HTML document. Is it not possible for the content of an iframe to be just plain text ??
2)文件内容包含在 pre 标签出于某种原因。为什么会这样?是不是总是这样?
2) The file content gets wrapped inside a pre tag for some reason. Why is this so ? Is it always the case?
3)我在javascript中的访问方法正在运行,但还有其他选择吗? [原生js解决方案请]如果内容总是包含在 pre 标记中,那么我实际上必须在 pre 中查找标记而非查找 innerHTML
3) My access method in the javascript is working but is there any other alternative? [native js solutions please] If the content is wrapped in a pre tag always then I will actually have to lookup inside the pre tag rather than lookup the innerHTML
推荐答案
innerHTML
不会返回元素的完全内容,它是一个返回HTML的非标准化方法>等同于到真实内容,在HTML中相当于纯文本是< pre> foo ...< / pre>
。
innerHTML
does not return the exact content of an element, its a non-standardized method that returns HTML that is equivalent to the real content, and in HTML the equivalent to plain text is <pre>foo...</pre>
.
你可能有更好的运气 innerText 属性..
You might have better luck with the innerText property..
这篇关于使用JavaScript阅读IFrame内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!