omg*_*omg 146 html javascript iframe
<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>
Run Code Online (Sandbox Code Playgroud)
我想得到的是:
test<br/>
Run Code Online (Sandbox Code Playgroud)
alg*_*thm 149
确切的问题是如何使用纯JavaScript而不是jQuery来实现它.
但我总是使用jQuery源代码中可以找到的解决方案.它只是一行原生JavaScript.
对我来说这是最好的,简单的可读性,甚至据我所知,以获得I帧内容的最短途径.
首先得到你的iframe
var iframe = document.getElementById('id_description_iframe');
// or
var iframe = document.querySelector('#id_description_iframe');
Run Code Online (Sandbox Code Playgroud)
然后使用jQuery的解决方案
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
Run Code Online (Sandbox Code Playgroud)
它甚至可以在Internet Explorer中工作,它
contentWindow在iframe对象的属性中执行此操作.大多数其他浏览器使用该contentDocument属性,这就是我们在此OR条件中首先证明此属性的原因.如果没有设置试试contentWindow.document.
在iframe中选择元素
然后你通常可以使用getElementById()甚至querySelectorAll()从以下位置选择DOM-Element iframeDocument:
if (!iframeDocument) {
throw "iframe couldn't be found in DOM.";
}
var iframeContent = iframeDocument.getElementById('frameBody');
// or
var iframeContent = iframeDocument.querySelectorAll('#frameBody');
Run Code Online (Sandbox Code Playgroud)
在iframe中调用函数
获取window元素iframe来调用一些全局函数,变量或整个库(例如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 /*, ... */);
Run Code Online (Sandbox Code Playgroud)
注意
如果您遵守同源政策,所有这一切都是可能的.
小智 68
使用JQuery,试试这个:
$("#id_description_iframe").contents().find("body").html()
Run Code Online (Sandbox Code Playgroud)
biz*_*zr3 38
它对我来说很完美:
document.getElementById('iframe_id').contentWindow.document.body.innerHTML;
Run Code Online (Sandbox Code Playgroud)
Jos*_*lio 23
AFAIK,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);
}
Run Code Online (Sandbox Code Playgroud)
Cou*_*Web 10
content在JS中使用iframe:
document.getElementById('id_iframe').contentWindow.document.write('content');
Run Code Online (Sandbox Code Playgroud)
小智 5
我认为在标签之间放置文本是为无法处理 iframe 的浏览器保留的,即..
<iframe src ="html_intro.asp" width="100%" height="300">
<p>Your browser does not support iframes.</p>
</iframe>
Run Code Online (Sandbox Code Playgroud)
您使用 'src' 属性来设置 iframes html 的源...
希望有帮助:)
Chalkey是正确的,您需要使用src属性来指定iframe中要包含的页面。如果您执行此操作,并且 iframe 中的文档与父文档位于同一域中,则可以使用以下命令:
var e = document.getElementById("id_description_iframe");
if(e != null) {
alert(e.contentWindow.document.body.innerHTML);
}
Run Code Online (Sandbox Code Playgroud)
显然,您可以对这些内容做一些有用的事情,而不仅仅是将它们放入警报中。
| 归档时间: |
|
| 查看次数: |
408797 次 |
| 最近记录: |