太扯了,一个多小时都没搞定,获取不到iframe中的dom元素。

 <div id="one">
this is one
</div>
<div>
<iframe src="./demo.html" id="test"> </iframe>
</div> <script type="text/javascript"> console.log(window.frames["test"].document.getElementById("two").innerHTML)//IE
console.log(document.getElementById("test").contentWindow.document.getElementById("two").innerHTML)//W3C
console.log(document.getElementById("test").contentDocument.getElementById("two").innerHTML)//W3C </script>

三个输出都是 null ,是什么原因呢?

把这段js写到window.onload=function(){}就可以啦。js执行的时候,iframe还没加载完成啦。

 <script type="text/javascript">
window.onload=function(){
var test=document.getElementById("demo").contentWindow.document.getElementById("test");
var one=document.getElementById("one");
console.log(test.ownerDocument.getElementsByTagName("P")[0].innerHTML);
console.log(one.ownerDocument.getElementsByTagName("P")[0].innerHTML);
console.log(test.ownerDocument===one.ownerDocument); //false
}
</script>

这个地方看出来iframe 中元素的ownerDocument 和 文档中元素的ownerDocument 是不同的

javascript 获取iframe中的dom的更多相关文章

  1. jquery获取iframe中的dom对象

    父窗口中操作iframe:$(window.frames["iframeChild"].document)    //假如iframe的id为iframeChild 在子窗口中操作 ...

  2. javascript获取iframe框架中页面document对象,获取子页面里面的内容,iframe获取父页面的元素,

    javascript获取iframe框架中,加载的页面document对象 因为浏览器安全限制,对跨域访问的页面,其document对象无法读取.设置属性 function getDocument(i ...

  3. javascript 获取iframe里页面中元素值的方法 关于contentWindow和contentDocumen

    javascript 获取iframe里页面中元素值的方法 IE方法:document.frames['myFrame'].document.getElementById('test').value; ...

  4. Jquery-获取iframe中的dom对象

    父窗口中操作iframe: $(window.frames["iframeChild"].document) //假如iframe的id为iframeChild 在子窗口中操作父窗 ...

  5. 【学习】如何用jQuery获取iframe中的元素

    (我的博客网站中的原文:http://www.xiaoxianworld.com/archives/292,欢迎遇到的小伙伴常来瞅瞅,给点评论和建议,有错误和不足,也请指出.) 说实在的,以前真的很少 ...

  6. Js/Jquery获取iframe中的元素

    转载: Js/Jquery获取iframe中的元素 - - ITeye技术网站http://java-my-life.iteye.com/blog/1275205 在web开发中,经常会用到ifram ...

  7. js 获取iframe中的元素

    今天要修改编辑器插件中的元素遇到的问题 jquery 在父窗口中获取iframe中的元素 1.Js代码 格式:$("#iframe的ID").contents().find(&qu ...

  8. 获取iframe中的元素

    父窗口中获取iframe中的元素 var ifr = document.getElementById('suggustion').contentWindow.document.body; 在ifram ...

  9. Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法

    在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素  1. 格式:window ...

随机推荐

  1. CentOS 7 安装 MySQL Database

    CentOS 7 安装 MySQL Database 1. 现在安装包,MySQL的安装包被分成了社区版和企业版,而本文将记录社区版本MySQL安装过程,下载MySQL版本如下: mysql-5.7. ...

  2. PPP 转义字符 编码 和 解码

    #include <stdio.h> #include <string.h> // PPP数据帧每一帧都以标识字符0x7E开始和结束: // 由于标识字符的值是0x7E,因此当 ...

  3. jQuery元素查找方式

    jQuery常用的元素查找方法总结 $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到 ...

  4. MySQL 重装

    由于之前第一次装MySQL,默认的datadir在启动盘中,我想要将datadir移动到更大的存储盘中.无奈网上的各种文章的方法在我这里总是不work.我决定重新用homebrew来装一遍MySQL. ...

  5. node.js Websocket消息推送---GoEasy

    Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送 速度快,代码简单易懂上手快 浏览器兼容性:GoEasy推送 支持websocket ...

  6. throw与throws的区别

    throws语句       throws总是出现在一个函数头中,用来标明该成员函数可能抛出的各种异常.对大多数Exception子类来说,Java   编译器会强迫你声明在一个成员函数中抛出的异常的 ...

  7. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  8. C++ CreateThread 实例

    //ThreadBase.h#pragma once #include<windows.h> class CThreadBase { public: CThreadBase(void); ...

  9. Postfix邮件服务器搭建及配置

    一.邮件服务器(Mail Server)的传输协议 1.简单邮件传输协议(SMTP):Simple Mail Transger Protocol 2.扩展的简单邮件传输协议(ESMTP):Extend ...

  10. pyqt4:线程的串联运行方式

    有些时候我们在pyqt中需要线程串行运行,而不是并发运行,用以下方式,这是在网上找的,暂存. > Hello > I have something like the foll scenar ...