在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. java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed

    错误描述: ElasticSearch集群启动错误,错误的原因是:因为Centos6不支持SecComp,而ES默认bootstrap.system_call_filter为true进行检测,所以导致 ...

  2. zabbix配置报警媒介-用户-动作-邮件脚本触发mailx邮件报警

    2018-09-16更新,新版本zabbix不需要使用脚本发送邮件,在zabbix web界面直接配置就可以 配置邮件参数,测试发送邮件 确认安装相关服务,centos7默认安装 [root@VM_1 ...

  3. Leecode刷题之旅-C语言/python-26.删除数组中的重复项

    /* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remo ...

  4. python爬虫 爬取steam热销游戏

    好久没更新了啊...最近超忙 这学期学了学python 感觉很有趣 就写着玩~~~ 爬取的页面是:https://store.steampowered.com/search/?filter=globa ...

  5. unity独立游戏开发日志2018/09/22

    f::很头痛之前rm做的游戏在新电脑工程打不开了...只能另起炉灶... 还不知道新游戏叫什么名...暂且叫方块世界.(素材已经授权) 首先是规划下场景和素材文件夹的建立. unity常用的文件夹有: ...

  6. HyperLedger Fabric 1.4 超级账本起源(5.1)

    至比特币开源以来,无数技术人员对其进行研究,并且对该系统经过了无数次改进,超级账本项目(Hyperledger)最初也是用来改善比特币的底层技术,最终由Linux基金会组织发展起来.       开放 ...

  7. SIMD数据并行(一)——向量体系结构

    在计算机体系中,数据并行有两种实现路径:MIMD(Multiple Instruction Multiple Data,多指令流多数据流)和SIMD(Single Instruction Multip ...

  8. Android开发——View绘制过程源码解析(二)

    0. 前言   View的绘制流程从ViewRoot的performTraversals开始,经过measure,layout,draw三个流程,之后就可以在屏幕上看到View了.上一篇已经介绍了Vi ...

  9. 程序在Linux下前后台切换

    程序在Linux下前后台切换 一.为什么要使程序在后台执行 背景:SecureCRT远程连接到linux主机,使程序在后台运行有以下好处: (1)本机关机不影响linux主机运行 (2)不影响计算效率 ...

  10. **leetcode笔记--4 Sum of Two Integers

    question: Calculate the sum of two integers a and b, but you are not allowed to use the operator + a ...