首先创建两个页面

//iframe1.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p id="content">帅哥天下9</p>
<script>
         console.log( window.parent.document.getElementById("testParent").innerHTML);
//调用父框架
         </script>


</body>
</html>
//demo1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
 <button id="btn">click</button>
<div id="testParent">调用父框架</div>
 <iframe src="iframe1.html" id="iframe1" frameborder="1"></iframe>

 <script>
var btn=document.getElementById("btn");
var iframe1=document.getElementById("ifram1");
btn.onclick=function(){ iframe1.contentWindow.document.getElementById("content").
style.background="red"; //iframe1.contentDocument.getElementById("content")
.style.background="red"; }
</script>
</body>
</html>

iframe1.contentWindow 获取 src设置页面的window对象然后操作里面的DOM

这个方法兼容IE 678 和其他主流浏览器 比如 FF Chrome 但是 Chrome对安全有保护

只可以在服务器端使用 可以用phpstudy测试

iframe1.contentDocument  IE低版本不支持

在Chrome同理

window.parent 调用父框架

window.top 调用顶层框架

//ifram2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body> <button id="changeTopDiv">changeTopDiv</button>
<iframe src="iframe2.html" frameborder="1" ></iframe>
<script>
var ctd=documet.getElementById("changeTopDiv");
var topDiv=window.top.document.getElementById("topIframe");
ctd.onclick=funtion(){

                topDiv.style.background="red";
}
</script> </body>
</html>
//demo2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<iframe src="iframe2.html" frameborder="1"></iframe> </body>
</html>
//demo3.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body> <div id="topIframe">topIframe</div>
<iframe src="demo2.html" frameborder="1"></iframe> </body>
</html>

还有一个就是防止钓鱼

有的网站会把别的网站iframe进来 然后欺骗用户去操作一些东西 谋利

code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<iframe src="test.html" frameborder="1"></iframe>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
if(window !=window.top){
//必须让当前页面为最高级别页面
window.top.location.href=window.location.href; }
</body>
</html>

改变框架高度

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
html,body{ padding: 0;
margin: 0;
} .box{ width:200px;
height:200px;
background: red; } </style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
html,body{ padding: 0;
margin: 0;
} .box{ width:200px;
height:400px;
background: green; }
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<iframe src="iframe5.html" frameborder="1" id="show" scrolling="no"></iframe>
<button id="btn1">btn1</button>
<button id="btn2">btn2</button>
<script>
var btn1=document.getElementById("btn1");
var btn2=document.getElementById("btn2");
var show=document.getElementById("show");
function changeHeight(){
setTimeout(function(){
// 添加一个定时器 让他执行慢一点
//不然src刚执行完 html 还没刷新完
// 就改变宽度 还是之前的宽度
show.height=show.contentWindow.document.body.offsetHeight; }, 200); } changeHeight(); btn1.onclick=function(){ show.src="iframe5.html";
changeHeight(); } btn2.onclick=function(){ show.src="iframe6.html";
changeHeight(); }
</script>
</body>
</html>

写到这里 累死我了

最后一个就是iframe 的load事件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<iframe src="iframe8.html" frameborder="1" id="show" scrolling="no"></iframe>

<script>
           var show=document.getElementById("show");
show.onload=function(){
alert("加载完毕!");
} //ie 也支持这个事件 但是 IE事件不能这么用
//得需要事件绑定才可以
//show.attachEvent("click",function(){ alert("加载完毕"); });
        </script>

     </script>
</body>
</html>

操作iframe 的方法与兼容性的更多相关文章

  1. jquery操作iframe的方法:父页面和子页面相互操作的方法

    今天在弄jquery操作iframe中元素:先由iframe中的子页面b.html给外面的父页面a.html页面传值,再将a.html页面计算机的值放到b.html页面上,这里就用到子页面和父页面相互 ...

  2. 操作iframe的方法

    子页面 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&q ...

  3. js学习笔记:操作iframe

    iframe可以说是比较老得话题了,而且网上也基本上在说少用iframe,其原因大致为:堵塞页面加载.安全问题.兼容性问题.搜索引擎抓取不到等等,不过相对于这些缺点,iframe的优点更牛,跨域请求. ...

  4. jquery 操作iframe的几种方法总结

    iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作 DOM方法: 父窗口操作IFRAME:window.frames["iframeSo ...

  5. JQuery操作iframe父页面与子页面的元素与方法

    JQuery操作iframe父页面与子页面的元素与方法 JQUERY IFRAME 下面简单使用Jquery来操作iframe的一些记录,这个使用纯JS也可以实现. 第一.在iframe中查找父页面元 ...

  6. 笔记:javascript操作iframe内的DOM元素,及调用iframe内的方法

    iframe相当于一个嵌入在网页内的浏览器,它与当前网页是隔离的. 但有时我们也需要在当前网页操作iframe内的元素或操作iframe内的javascript方法. 在网页内操作DOM元素,是使用d ...

  7. jQuery操作iframe中js函数的方法小结

    1.jquery操作iframe中的元素(2种方式) ? 1 2 var tha = $(window.frames["core_content"].document).find( ...

  8. jquery方法操作iframe元素

    操作iframe父元素 $("#rolesCtl",parent.document).find( 'button' ).trigger( 'click' ); 在父页面获取ifra ...

  9. 操作iframe的一些方法

    //父页面操作iframe里的内容 oInput.onclick=function(){ var oBox = oIframe.contentWindow.document.getElementByI ...

随机推荐

  1. CSS图片下面产生间隙的6种解决方案

    CSS图片下面产生间隙的6种解决方案 在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对於 该问题的解决方法也是 ...

  2. Integer中1000==1000为false而100==100为true

    查看Integer.java类,会发现有一个内部私有类,IntegerCache.java,它缓存了从-128到127之间的所有的整数对象.如果在这个区间内,他就会把变量当做一个变量,放到内存中:但如 ...

  3. object-c 数学计算公式

    1. 三角函数  double sin (double);正弦  double cos (double);余弦  double tan (double);正切  2 .反三角函数  double as ...

  4. c#调用c++ dll 入坑记录

    1.DLL引用坑 [DllImport("NetDLL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConve ...

  5. 爬虫、框架scrapy

    阅读目录 一 介绍 二 安装 三 命令行工具 四 项目结构以及爬虫应用简介 五 Spiders 六 Selectors 七 Items 八 Item Pipeline 九 Dowloader Midd ...

  6. (转载)ibatis:解决sql注入问题

    原文地址:http://blog.csdn.net/scorpio3k/article/details/7610973 对于ibaits参数引用可以使用#和$两种写法,其中#写法会采用预编译方式,将转 ...

  7. DOM事件阶段以及事件捕获与事件冒泡先后执行顺序

    平时浏览这么多技术文章,如过不去实践.深入弄透它,这个技术点很快就会在脑海里模糊.要加深印象,就得好好过一遍.重要的事情说三遍,重要的知识写一遍. 开发过程中我们都希望使用别人成熟的框架,因为站在巨人 ...

  8. Extjs Window用法详解 2 打印具体应用

    Extjs 中的按钮元素 { xtype: 'buttongroup', title: '打印', items: [ me.tsbDel = Ext.create('Ext.button.Button ...

  9. spring各个版本源码

    各版本源码下载地址 http://maven.springframework.org/release/org/springframework/spring/

  10. hdu 6118度度熊的交易计划(费用流)

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...