Elton's Blog

jQuery控制iframe中对象的方法

by on 七.17, 2009, under Web

jQuery中的$()方法很容易获取到DOM中的元素。但是这个方法不适用于引用iframe中的元素。 如下面的html
a.htm

1
<iframe id="frame" src="http://www.prosight.me/b.htm" frameborder="0"></iframe>

b.htm

1
<div id="default">this is b.htm</div>

如果要在a.htm中获得b.htm中的id为default的div,可以使用如下方法:

1
$("#frame").contents().find("#default")

来得到这个div的jQuery对象。

特别需要注意一点,就是iframe中的src一定要使用绝对路径,否则jQuery是无法正确找到对应的元素的。 尤其在公网上。之前就因为这个问题,折腾了一晚没解决,换成绝对地址就好了。





:,

1 Comment for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...