Example:
iframe.html
<a href="http://www.google.com">Google</a>
bla bla bla
<a href="http://www.yahoo.com">Yahoo</a>
index.html
<script>
...
</script>
There are the links from "iframe.html"
http://www.google.com
http://www.yahoo.com
Example:
iframe.html
<a href="http://www.google.com">Google</a>
bla bla bla
<a href="http://www.yahoo.com">Yahoo</a>
index.html
<script>
...
</script>
There are the links from "iframe.html"
http://www.google.com
http://www.yahoo.com
var links = $('iframe:first').contents()[0].links;
...或者不用jQuery...
var iframe = document.getElementsByTagName('iframe')[0],
doc = iframe.contentDocument || iframe.contentWindow.document;
var links = doc.links;
这利用了document.links
属性。
$.each()
函数,所以与直接从contents()
选择器迭代锚点相比,实际上并没有节省太多工作量,对吧? - undefineda
个元素,并且只返回具有 href
属性的 a
元素。 - undefined$("#my_iframe").contents().find('a').each({
$('#results').append($(this).attr('href') + '<br />');
});
花点时间了解一下JQuery的.contents()函数。
#results
添加的方式有点麻烦。为什么不用append()
呢? - undefined
iframe.html
和index.html
的域名、协议和端口是否相同? - undefined