利用iframe进行父页面和子页面的通信
2017-08-17 09:43
295 查看
父页面parent.html:
<span id='parentSpan'>父页面元素</span>
<iframe src="child1.html" id='frame1'></iframe><br/>
<iframe src="child2.html" id='frame2'></iframe><br/>
<button onclick="getChildItem()">调用子页面方法</button>
<button onclick="getChildItem()">获取子页面元素</button>
调用子页面child1.html的方法,并获取其元素
function parentSay(){
alert('say parent')
}
function getChildItem(){
var child=document.getElementById('frame1').contentWindow;//第一种方法获取子页面
//调用子页面的方法
child.childSay();
}
function getChildItem(){
var child=window.frames[0];//第二种方法获取子页面
var childSpan=child.document.getElementById('childSpan');//获取子页面的元素
alert(childSpan.innerHTML)
}
子页面1, child1.html:
<span id='childSpan'>子页面1的元素</span><br/>
<button onclick="getParentFunction()">调用父页面方法</button>
<button onclick="getParentItem()">获取父页面元素</button>子页面的方法:
function childSay(){
alert('page1 function')
}
function getParentFunction(){
var parent=window.parent;//获取父页面
parent.parentSay();//调用父页面的方法
}
function getParentItem(){
var parent=window.parent;
var parentSpan=parent.document.getElementById('parentSpan');//获取父页面的元素
alert(parentSpan.innerHTML)
}
子页面1也可以与子页面2进行通信
child1中添加
function getPage3(){
var parent=window.par
4000
ent;//这里可以用parent.window获取父页面,在chrome测试过
var page3=parent.document.getElementById('frame2').contentWindow;//通过父页面获取到父页面中的另一个子页面
page3.childSay();//调用另一个子页面的方法
}
<span id='parentSpan'>父页面元素</span>
<iframe src="child1.html" id='frame1'></iframe><br/>
<iframe src="child2.html" id='frame2'></iframe><br/>
<button onclick="getChildItem()">调用子页面方法</button>
<button onclick="getChildItem()">获取子页面元素</button>
调用子页面child1.html的方法,并获取其元素
function parentSay(){
alert('say parent')
}
function getChildItem(){
var child=document.getElementById('frame1').contentWindow;//第一种方法获取子页面
//调用子页面的方法
child.childSay();
}
function getChildItem(){
var child=window.frames[0];//第二种方法获取子页面
var childSpan=child.document.getElementById('childSpan');//获取子页面的元素
alert(childSpan.innerHTML)
}
子页面1, child1.html:
<span id='childSpan'>子页面1的元素</span><br/>
<button onclick="getParentFunction()">调用父页面方法</button>
<button onclick="getParentItem()">获取父页面元素</button>子页面的方法:
function childSay(){
alert('page1 function')
}
function getParentFunction(){
var parent=window.parent;//获取父页面
parent.parentSay();//调用父页面的方法
}
function getParentItem(){
var parent=window.parent;
var parentSpan=parent.document.getElementById('parentSpan');//获取父页面的元素
alert(parentSpan.innerHTML)
}
子页面1也可以与子页面2进行通信
child1中添加
function getPage3(){
var parent=window.par
4000
ent;//这里可以用parent.window获取父页面,在chrome测试过
var page3=parent.document.getElementById('frame2').contentWindow;//通过父页面获取到父页面中的另一个子页面
page3.childSay();//调用另一个子页面的方法
}
相关文章推荐
- 利用JS对iframe父子(内外)页面进行操作的方法教程
- js对iframe内外(父子)页面进行操作
- 利用代理页面解决html iframe跨域访问网站问题
- 在LiveCode中利用socket进行通信(1)--入门介绍
- js前端进行同一域名或不同域名下两个页面跨域通信
- php用户注册页面利用js进行表单验证
- Java中利用Socket进行通信
- 利用多线程进行多客户端通信
- 使用Vue开发网站之路2-多组件通信1(利用bus总线进行事件触发)
- iOS 利用Router机制进行动态页面跳转
- jsp页面利用 fmt 标签进行时间 的转换
- c#利用TCP/IP协议与川崎机器人进行通信
- 利用管道进行通信
- 利用Qt进行接口间通信
- 嵌套iframe下父子页面之间的通信
- 开启本地服务器,利用二维码进行通信[Demo]
- 页面分页技术(一)-------------完全利用JSP进行分页
- jsp页面中带有iframe页面进行后台传值出现中文乱码
- 在VB中利用API进行串口通信
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例