Get HTML inside iframe using jQuery

后端 未结 10 1109
清歌不尽
清歌不尽 2020-11-27 04:24

Here is my situation.

I have a file called iframe.html, which contains the code to for a image slideshow. The code is somewhat like

<         


        
相关标签:
10条回答
  • 2020-11-27 05:06

    Try this code:

    $('#iframe').contents().find("html").html();

    This will return all the html in your iframe. Instead of .find("html") you can use any selector you want eg: .find('body'),.find('div#mydiv').

    0 讨论(0)
  • 2020-11-27 05:06
    $('#iframe').load(function() {
        var src = $('#iframe').contents().find("html").html();
        alert(src);
    });
    
    0 讨论(0)
  • 2020-11-27 05:08

    This line will retrieve the whole HTML code of the frame. By using other methods instead of innerHTML you can traverse DOM of the inner document.

    document.getElementById('iframe').contentWindow.document.body.innerHTML
    

    Thing to remember is that this will work only if the frame source is on the same domain. If it is from a different domain, cross-site-scripting (XSS) protection will kick in.

    0 讨论(0)
  • 2020-11-27 05:13
    var iframe = document.getElementById('iframe');
      $(iframe).contents().find("html").html();
    
    0 讨论(0)
提交回复
热议问题