Example:
iframe.html
<a href="">Google</a>
bla bla bla
<a href="">Yahoo</a>
index.html
<script>
...
</script>
There are the links from "iframe.html"
Example:
iframe.html
<a href="http://www.google.">Google</a>
bla bla bla
<a href="http://www.yahoo.">Yahoo</a>
index.html
<script>
...
</script>
There are the links from "iframe.html"
http://www.google.
http://www.yahoo.
Share
Improve this question
asked Jul 14, 2011 at 5:16
hugohugo
372 silver badges4 bronze badges
4
- Is index.html and iframe.html in the same domain? – wong2 Commented Jul 14, 2011 at 5:21
-
Does
iframe.html
have the same domain, protocol and port asindex.html
? – alex Commented Jul 14, 2011 at 5:22 - See this answer. detect url's in text with Javascript – Adeel Commented Jul 14, 2011 at 5:22
- @Adeel: That is detecting URLs in strings. The OP wants links in the DOM. – alex Commented Jul 14, 2011 at 5:31
2 Answers
Reset to default 4If 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.
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745367143a4624635.html
评论列表(0条)