Jquery取得iframe中元素的几种方法

来源:互联网 发布:新手开淘宝店视频教程 编辑:程序博客网 时间:2024/05/21 09:14

W3C的标准告诉我们,可以通过Dom对象的contentDocument属性来返回文档对象。

var doc = document.getElementById('mainFrame' ).contentDocument

IE8开始支持,如果你的项目不用兼容IE6,IE7的话使用这种方式最好。

IE6,IE7需要如此访问

var doc = document.frames['mainFrame'].document;

兼容方式:

var doc = document.getElementById('mainFrame' ).contentDocument || document.frames['mainFrame'].document;

以上是Javascript原生方法:

使用Jquery则简单些

$("iframe").contents().find("selector")

试试这样写:

$('#frameID').load(function () {
    $('#frameID').contents().find('a');//以iframe中找元素a为例
});
0 0
原创粉丝点击