How extract links from iframe using javascript

前端 未结 2 2010
悲哀的现实
悲哀的现实 2020-12-21 22:22

Example:

iframe.html

Google
bla bla bla
Yahoo


        
相关标签:
2条回答
  • 2020-12-21 22:46

    If the domain, protocol and ports match, just use...

    var links = $('iframe:first').contents()[0].links;
    

    jsFiddle.

    ...or without jQuery...

    var iframe = document.getElementsByTagName('iframe')[0],
        doc = iframe.contentDocument || iframe.contentWindow.document; 
    
    var links = doc.links;
    

    jsFiddle.

    This takes advantage of the document.links property.

    0 讨论(0)
  • 2020-12-21 22:55

    Assuming your iframe is on the same domain as your website, and has the id "my_iframe" and you have a div with the id "results", this should work for you:

    $("#my_iframe").contents().find('a').each({
        $('#results').append($(this).attr('href') + '<br />');
    });
    

    Take a moment to read up on JQuery's .contents() function.

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