简体   繁体   English

如何使用 javascript 从 iframe 中提取链接

[英]How extract links from iframe using javascript

Example:例子:

iframe.html iframe.html

<a href="http://www.google.com">Google</a>
bla bla bla
<a href="http://www.yahoo.com">Yahoo</a>

index.html索引.html

<script>
...
</script>
There are the links from "iframe.html"
http://www.google.com
http://www.yahoo.com

If the domain, protocol and ports match, just use...如果域、协议和端口匹配,只需使用...

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

jsFiddle . js小提琴

...or without jQuery... ...或没有 jQuery...

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

var links = doc.links;

jsFiddle . js小提琴

This takes advantage of the document.links property.这利用了document.links属性。

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:假设您的 iframe 与您的网站在同一个域上,并且具有 id“my_iframe”并且您有一个 id 为“results”的 div,这应该适合您:

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

Take a moment to read up on JQuery's .contents() function.花点时间阅读 JQuery 的.contents() function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM