The dom of instance in the iframe of JS operation is explained
- 2020-03-30 01:37:05
- OfStack
Direct assignment of the following code test can be seen:
1. The HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Headless document </title>
</head>
<body>
<div class="line">==================== Pay attention to : The test starts here =========================</div>
<p id="pox"> Used to test subforms iframeA Access an element of the parent form </p>
<div class="line">====================iframe The divider =========================</div>
<iframe src="a.html" width="100%" frameborder="0" id="frameA" name="frameA"></iframe>
<iframe src="b.html" name="iframeB" width="100%" frameborder="0" id="frameB"></iframe>
<div class="line">====================iframe The divider =========================</div>
<p> First to demonstrate : The parent form accesses a method or element in a child form </p>
<p> conclusion : The parent form accesses the child form in a different way than the element </p>
<input type="button" onclick="frameDiv()" value=" The parent form accesses an element in the child form " />
<input type="button" onclick="frameFun()" value=" The parent form accesses a method in a child form " />
<script type="text/javascript">
//The child window accesses the parent window method
function testP(ss){
alert(ss)
}
//Gets the element of the iframe
function getIframe(id){
return document.getElementById(id).contentWindow.document;
}
//The parent window accesses the child window element
function frameDiv(){
getIframe("frameA").getElementById("ooxx").style.backgroundColor="#f00"
//window.frames["iframeA"].getElementById("ooxx").style.backgroundColor="#f00" // You cannot access an element in this form
}
//The parent window accesses the child window method
function frameFun(){
//getIframe("frameB").getsFun();// A subform method cannot be accessed in this form
// window.frames["iframeB"].getsFun();
alert(window.frames["iframeB"].getsFun());
}
</script>
</body>
</html>
A.h HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Headless document </title>
</head>
<body>
<div id="ooxx"> Used to test that a parent form accesses an element in a child form </div>
<p id="divooxx"> To test the child window B To access the form A Some element of the </p>
<p>1. Child window iframeA Access an element of the parent window </p>
<input type="button" onclick="frameToPdiv()" value="The child window accesses an element of the parent window" />
<input type="button" onclick="frameToPfun()" value=" A child window accesses a method of the parent window " />
<script type="text/javascript">
//The child window accesses an element of the parent window
function frameToPdiv(){
parent.document.getElementById("pox").style.color="#fff";
parent.document.getElementById("pox").style.backgroundColor="#f0a0f0"
}
//The child window accesses the parent window method
function frameToPfun(ss){
parent.testP("ssss");
}
//Method used to test access to iframeB
function testBA(){
alert("Method used to test access to iframeB")
}
</script>
</body>
</html>
B.h HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Headless document </title>
</head>
<body>
<p> two : Test that child forms access a method or element to each other </p>
<input type="button" value="Subform B accesses an element of subform A" onclick="frameTframeDiv()" />
<input type="button" value="Subform B accesses A method of subform A" onclick="frameTframeFun()" />
<script type="text/javascript">
//Subform B accesses an element of subform A
function frameTframeDiv(){
//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");//The child form accesses the parent form method
_bframe.getElementById("divooxx").style.color="#a0c0f0";
_bframe.getElementById("divooxx").style.backgroundColor="#000";
}
//Subform B accesses A method of subform A
function frameTframeFun(){
window.parent.frames["frameA"].testBA();
}
</script>
<script type="text/javascript">
function getsFun(){
return "sssssss";
}
//getFun()
</script>
</body>
</html>