Get data from iframe

前端 未结 4 831
遇见更好的自我
遇见更好的自我 2020-12-20 13:05

I am doing this first time. I have created an iframe on my page and I want the text from the iframe through jquery. Here is my code :<

相关标签:
4条回答
  • 2020-12-20 13:16

    The contentWindow works in both FF and chrome

    document.getElementById('myFrame').contentWindow.document.body    
    

    Would give you a DOM element body

    You can also try something like

     window.frames['myIframe'].document.body    
    

    That might do the trick for you also

    You might have problems with your browsers built in security. If you run this on a local machine. There is a way to disable browsers security.

    0 讨论(0)
  • 2020-12-20 13:28
    var content=$("iframe").contents().find('body').html();
    
    alert(content);
    
    0 讨论(0)
  • 2020-12-20 13:29

    Use .contents() to get to iFrame's DOM.

    $('#myIframe').contents()
    

    UPDATE:

    In the OP:

    $("#result").html(iframeContent.find('body').html);
    

    Should say:

    $("#result").html(iframeContent.find('body').html());
    
    0 讨论(0)
  • 2020-12-20 13:35

    Doing with jquery will be a little easier:

    $('Your Selector', frames['myIframe'].document)
    

    The above example will get anything from myIframe. But the iframe MUST be from the same domain as the parent document. If not from the same domain, a security violation occurs (You can't add content from foreign sites to your page and change that content.)

    If no security violation, you can do anything with the selection. For example you can use the jquery append() method to insert new html inside the iFrame, you can use the html() method to replace html or any other function that jquery/pure javascript allows.

    0 讨论(0)
提交回复
热议问题