<iframe id="id_description_iframe" class="rte-zone" height="200" frameborder="0" title="description">
<html>
<head></head>
<body class="frameBody">
test<br/>
</body>
</html>
</iframe>
我想要得到的是:
test<br/>
<iframe id="id_description_iframe" class="rte-zone" height="200" frameborder="0" title="description">
<html>
<head></head>
<body class="frameBody">
test<br/>
</body>
</html>
</iframe>
我想要得到的是:
test<br/>
确切的问题是如何使用纯JavaScript而不是jQuery来实现。
但我总是使用可以在jQuery源代码中找到的解决方案。 这只是一行本地JavaScript代码。
对我来说,这是最好的、易于阅读的,甚至是获取iframe内容最短的方法。
首先获取你的iframe
var iframe = document.getElementById('id_description_iframe');
// or
var iframe = document.querySelector('#id_description_iframe');
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
即使是Internet Explorer,它也可以通过
iframe
对象的contentWindow
属性来实现此操作。大多数其他浏览器使用contentDocument
属性,这就是为什么我们首先在此OR条件中证明此属性的原因。如果未设置,请尝试使用contentWindow.document
。
选择iframe中的元素
然后,您通常可以使用getElementById()
甚至querySelectorAll()
从iframeDocument
选择DOM元素:
if (!iframeDocument) {
throw "iframe couldn't be found in DOM.";
}
var iframeContent = iframeDocument.getElementById('frameBody');
// or
var iframeContent = iframeDocument.querySelectorAll('#frameBody');
在iframe中调用函数
仅获取iframe
中的window
元素,以调用一些全局函数、变量或整个库(例如jQuery
):
var iframeWindow = iframe.contentWindow;
// you can even call jQuery or other frameworks
// if it is loaded inside the iframe
iframeContent = iframeWindow.jQuery('#frameBody');
// or
iframeContent = iframeWindow.$('#frameBody');
// or even use any other global variable
iframeWindow.myVar = window.myVar;
// or call a global function
var myVar = iframeWindow.myFunction(param1 /*, ... */);
注意
如果您遵守同源策略,所有这些都是可能的。
document.querySelector('#id_description_iframe').onload = function() {...
希望对某些人有所帮助。 - user3442612使用jQuery,可以尝试以下代码:
$("#id_description_iframe").contents().find("body").html()
对我来说它完美地工作:
document.getElementById('iframe_id').contentWindow.document.body.innerHTML;
.body
无效。然而,.getElementsByTagName('body')[0]
有效。 - Krisztián Balla据我所知,Iframe不能以那种方式使用。您需要将其src属性指向另一个页面。
以下是使用普通 JavaScript 获取其主体内容的方法。这适用于 IE 和 Firefox。
function getFrameContents(){
var iFrame = document.getElementById('id_description_iframe');
var iFrameBody;
if ( iFrame.contentDocument )
{ // FF
iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
}
else if ( iFrame.contentWindow )
{ // IE
iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
}
alert(iFrameBody.innerHTML);
}
如何在使用JS的iframe中使用content
:
document.getElementById('id_iframe').contentWindow.document.write('content');
我认为在<noframes>标签中放置文本是为了适应无法处理iframe的浏览器,例如...
<iframe src ="html_intro.asp" width="100%" height="300">
<p>Your browser does not support iframes.</p>
</iframe>
Chalkey是正确的,您需要使用src属性来指定要包含在iframe中的页面。只要您这样做,并且iframe中的文档与父文档在同一个域中,您就可以使用以下内容:
var e = document.getElementById("id_description_iframe");
if(e != null) {
alert(e.contentWindow.document.body.innerHTML);
}
var iframe = document.getElementById("adblock_iframe");
2.创建一个您想要插入到框架中的元素,比如一张图片:
var img = document.createElement('img');
img.src = "https://server-name.com/upload/adblock" + id + ".jpg";
img.style.paddingLeft = "450px";
//scale down the image is we have a high resolution screen on the client side
if (retina_test_media == true && high_res_test == true) {
img.style.width = "200px";
img.style.height = "50px";
} else {
img.style.width = "400px";
img.style.height = "100px";
}
img.id = "image";
3.将图像元素插入到iframe中:
iframe.contentWindow.document.body.appendChild(img);
<iframe id="iframeId" name="iframeId">...</iframe>
<script type="text/javascript">
var iframeDoc;
if (window.frames && window.frames.iframeId &&
(iframeDoc = window.frames.iframeId.document)) {
var iframeBody = iframeDoc.body;
var ifromContent = iframeBody.innerHTML;
}
</script>
window.frames.iframeId.document
未定义。 - Ionică Bizău我已经尝试了以下代码:
var frameObj = document.getElementById('id_description_iframe');
var frameContent = frameObj.contentWindow.document.body.innerHTML;
其中"id_description_iframe"是您的iframe的id。 这段代码对我来说运行得很好。