在Web开发时,很多时候会遇到一个问题。我在一个页面嵌入了iframe,并且我想获得这个iframe页面某个元素的值。那么该如何实现这个需求呢?

先来看下演示:

效果演示

iframe1中文本框的值:

在IE下操作IFrame内容的代码:

1 document.frames["MyIFrame"].document.getElementById("s").style.color="blue";

但是这在Firefox下无效。所以,想到在Firefox下用FireBug来调试。经过调试发现在Firefox下可用以下代码来实现:

1 document.getElementById("MyIFrame").contentDocument.getElementById("s").style.color="blue";

demo代码:

01     <div><iframe name="frame1" id="frame1" src="frm.html" frameborder="1"height="60"></iframe></div>
02    
03     <p>iframe1中文本框的值:<input type="button" name="Submit"value="getValue" onclick="getValue()" /></p>
04      
05 <script type="text/javascript">
06 function getValue()
07 {
08     var ofrm1 = document.getElementById("frame1").document;   
09     if (ofrm1==undefined)
10     {
11         ofrm1 = document.getElementById("frame1").contentWindow.document;
12         var ff = ofrm1.getElementById("txt1").value;
13         alert("firefox/chrome取值结果为:" + ff);
14     }
15     else
16     {
17         var ie = document.frames["frame1"].document.getElementById("txt1").value;
18         alert("ie取值结果为:" + ie);
19     }
20 }
21 </script>

iframe页面代码:

01 <html>
02 <head>
03     <title>框架内页</title>
04 </head>
05 <body>
06     <div>
07         <input id="txt1" name="txt1" type="text" value="欢迎访问www.nowamagic.net" />
08     </div>
09 </body>
10 </html>

如何获取iframe DOM的值的更多相关文章

  1. Jquery 方式获取 iframe Dom元素

    Jquery 方式获取 iframe Dom元素 測试页面代码: <html>  <head>   <title>jquery方式,訪问iframe页面dom元素& ...

  2. 页面中获取 iframe 中的值

    3.页面中获取 iframe 中的值 var obj=document.getElementsByClassName(".ke-edit-iframe").contentWindo ...

  3. 父页面内获取获取iframe内的变量或者是获取iframe内的值

    前提:页面不可跨域访问,必须同一域名下,否则返回值为空 父页面 <!DOCTYPE html> <html lang="en"> <head> ...

  4. javascript_获取iframe框架中元素节点的属性值

    1. DOM:文档对象模型 [window 对象] 它是一个顶层对象,而不是另一个对象的属性即浏览器的窗口. [document 对象] 该对象是window和frames对象的一个属性,是显示于窗口 ...

  5. 在iframe中获取本iframe DOM引用

    window.frameElement 获取本iframe DOM window.frameElement.contentDocument.getElementById('id') 获取这个ifram ...

  6. 页面间(窗口间)的取值赋值及获取iframe下的window对象

    ①同一个窗口中,获取某个iframe的信息 <body> <iframe id="PAID" name="PA" src="Item ...

  7. jquery获取iframe中的dom对象

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

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

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

  9. vue3 template refs dom的引用、组件的引用、获取子组件的值

    介绍 通过 ref() 还可以引用页面上的元素或组件. DOM 的引用 <template> <div> <h3 ref="h3Ref">Tem ...

随机推荐

  1. python七类之列表元组

    列表 一.关键字:  list  lst = [ , , , , , , ,] lst = [1,2,3,4] 二.方法: 1.增加:​ . append( ) #追加​​​,添加元素进列表最后 ls ...

  2. 网站apache环境S2-057漏洞 利用POC 远程执行命令漏洞复现

    S2-057漏洞,于2018年8月22日被曝出,该Struts2 057漏洞存在远程执行系统的命令,尤其使用linux系统,apache环境,影响范围较大,危害性较高,如果被攻击者利用直接提权到服务器 ...

  3. Redis缓存数据库的安装与配置(1)

    1.安装 tarxf redis-3.2.5.tar.gz cd redis-3.2.5 make mkdir -p /usr/local/redis/bin src目录下这些文件作用如下 redis ...

  4. 003---设计首页index页面

    在项目的urls.py文件添加一条url from django.contrib import admin from django.urls import path, re_path from app ...

  5. PATA1034题解

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 参考:算法笔记(胡凡)10.3.1 # ...

  6. Hive数据倾斜和解决办法

    转自:https://blog.csdn.net/xinzhi8/article/details/71455883 操作: 关键词 情形      后果 Join 其中一个表较小,但是key集中   ...

  7. django生产环境中部署

    https://www.cnblogs.com/chenice/p/6921727.html 本节内容 uwsgi 介绍 uwsgi安装使用 nginx安装配置 django with nginx 如 ...

  8. CSS里一个奇怪的属性

    事情是这样的,在一个手机界面的制作中,我发现按钮点击后总会出现一个边框,于是开始搜索解决方案.搜到的解决方案是这样的. a:focus,input:focus{ -webkit-tap-highlig ...

  9. APP功能性测试-3

    定义:兼容测试就是指软件在特定的硬件平台,不同的应用软件之间,不同的操作系统平台上,不同的网络等环境中是否能够正常的运行的测试  (会不会产生不兼容) 兼容性测试的作用 进一步提高产品质量 和其他软件 ...

  10. %matplotlib inline

    整理摘自 https://zhidao.baidu.com/question/1387744870700677180.html %matplotlib inline是jupyter notebook里 ...