开发者

How to get content of subframe?

How to retrieve all the HTML code (including head and body tags) of a frame that is inside an iframe (so my JS firstly connects to the iframe and then connect to the frame开发者_如何学Go to get all the HTML code)? (All the pages are on the same domain.)


I'm not sure what a 'frame inside an iframe' is, but if you want to extract contents from an iframe you can use this code:

var iframe = document.getElementById("IFRAMEID");
var iframe_html = iframe.contentWindow.document;

Replace IFRAMEID by the id you give to the iframe. If you are using jQuery, you can use the $-selector of jQuery to do the same:

var content = $("#IFRAMEID").contentWindow.document;
var content = $("#IFRAMEID").contents(); // Both are correct;

If you want to extract only the body part (or manipulate it), you can extract the body part of the document like so:

var iframe = document.getElementById("IFRAMEID");
var content = iframe.contentWindow.document.body.innerHTML;

Now you can use content to read the HTML or you can write to the innerHTML and manipulate the content of the iframe itself.


Try using jQuery's .contents() function (http://api.jquery.com/contents/).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜