JS操作iframe里的dom(实例讲解)
直接赋值如下代码测试即可明白:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>无标题文档</title>
</head>
<body>
<divclass="line">====================注意:测试从这里开始=========================</div>
<pid="pox">用来测试子窗体iframeA访问父窗体的某元素</p>
<divclass="line">====================iframe分割线=========================</div>
<iframesrc="a.html"width="100%"frameborder="0"id="frameA"name="frameA"></iframe>
<iframesrc="b.html"name="iframeB"width="100%"frameborder="0"id="frameB"></iframe>
<divclass="line">====================iframe分割线=========================</div>
<p>先来演示:父窗体访问子窗体中的某方法或元素</p>
<p>总结:父窗体访问子窗体的方法跟元素采用不同的方式</p>
<inputtype="button"onclick="frameDiv()"value="父窗体访问子窗体中的某元素"/>
<inputtype="button"onclick="frameFun()"value="父窗体访问子窗体中的某方法"/>
<scripttype="text/javascript">
//子窗口访问父窗口方法
functiontestP(ss){
alert(ss)
}
//取得iframe的元素
functiongetIframe(id){
returndocument.getElementById(id).contentWindow.document;
}
//父窗口访问子窗口元素
functionframeDiv(){
getIframe("frameA").getElementById("ooxx").style.backgroundColor="#f00"
//window.frames["iframeA"].getElementById("ooxx").style.backgroundColor="#f00" //不能通过这种形式访问某元素
}
//父窗口访问子窗口方法
functionframeFun(){
//getIframe("frameB").getsFun();//不能通过这种形式访问子窗体某方法
//window.frames["iframeB"].getsFun();
alert(window.frames["iframeB"].getsFun());
}
</script>
</body>
</html>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>无标题文档</title>
</head>
<body>
<divid="ooxx">用来测试父窗体访问子窗体中的某元素</div>
<pid="divooxx">用来测试子窗口B访问窗体A的某元素</p>
<p>1.子窗口iframeA访问父窗口的某元素</p>
<inputtype="button"onclick="frameToPdiv()"value="子窗口访问父窗口的某元素"/>
<inputtype="button"onclick="frameToPfun()"value="子窗口访问父窗口的某方法"/>
<scripttype="text/javascript">
//子窗口访问父窗口的某元素
functionframeToPdiv(){
parent.document.getElementById("pox").style.color="#fff";
parent.document.getElementById("pox").style.backgroundColor="#f0a0f0"
}
//子窗口访问父窗口方法
functionframeToPfun(ss){
parent.testP("ssss");
}
//用于测试iframeB访问的方法
functiontestBA(){
alert("用于测试iframeB访问的方法")
}
</script>
</body>
</html>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>无标题文档</title>
</head>
<body>
<p>二:测试子窗体间相互访问某方法或元素</p>
<inputtype="button"value="子窗体B访问子窗体A的某元素"onclick="frameTframeDiv()"/>
<inputtype="button"value="子窗体B访问子窗体A的某方法"onclick="frameTframeFun()"/>
<scripttype="text/javascript">
//子窗体B访问子窗体A的某元素
functionframeTframeDiv(){
//parent.document.getElementById("frameA").contentWindow.document.getElementById("divooxx").style.color="#a0c0f0";
//parent.document.getElementById("frameA").contentWindow.document.getElementById("divooxx").style.backgroundColor="#000"
var_bframe=parent.getIframe("frameA");//子窗体访问父窗体方法
_bframe.getElementById("divooxx").style.color="#a0c0f0";
_bframe.getElementById("divooxx").style.backgroundColor="#000";
}
//子窗体B访问子窗体A的某方法
functionframeTframeFun(){
window.parent.frames["frameA"].testBA();
}
</script>
<scripttype="text/javascript">
functiongetsFun(){
return"sssssss";
}
//getFun()
</script>
</body>
</html>
相关文章
- js返回顶部
- 【说站】js中replaceAll方法的实现
- 物理世界的互动之旅:Matter.js入门指南
- 使用JS实现Redis数据读取(js读取redis)
- 使用 JS 操作 Redis:实现强大数据存储.(js调用redis)
- 两个DIV等高的JS的实现代码
- jquery简单图片导航插件jquery.imgNav.js
- js/ajax跨越访问-jsonp的原理和实例(javascript和jquery实现代码)
- JS模拟的腾讯微博app撕纸效果的实例代码
- js简易namespace管理器实例代码
- JS操作Cookie写入和读取实例代码
- ie中js创建checkbox默认选中问题探讨
- JS中实现replaceAll的方法(实例代码)
- JS简单实现文件上传实例代码(无需插件)
- JS小功能(onmouseover实现选择月份)实例代码
- js获取url参数代码实例分享(JS操作URL)
- JS判断文本框内容改变事件的简单实例
- JS异常处理try..catch语句的作用和实例
- node.js正则表达式获取网页中所有链接的代码实例
- js实现jquery的offset()方法实例