Introduction to the DOM in JS Operation IFrame

Source: Internet
Author: User

This article is mainly on the JS operation of the DOM is introduced, the need for friends can come to the reference, I hope to help you

Directly assigned to the following code test can be understood:

1.html:

The code is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>

<title> Untitled Document </title>

<body>

<div class= "line" >==================== Note: The test starts here =========================</div>

<p id= "pox" > Used to test Subform Iframea access to an element of the parent form </p>

<div class= "line" >====================iframe segment =========================</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 segment =========================</div>

<p> first to demonstrate that a parent form accesses a method or element in a subform </p>

<p> Summary: The parent form accesses the subform in a different way than the element </p>

<input type= "button" onclick= "Framediv ()" value= "parent form to access an element in a subform"/>

<input type= "button" onclick= "Framefun ()" value= "parent form to access a method in a subform"/>

<script type= "Text/javascript" >

child window Access Parent window method

function TESTP (ss) {

Alert (SS)

}

Get the elements of an IFRAME

function Getiframe (ID) {

return document.getElementById (ID). contentwindow.document;

}

Parent window Access child window element

function Framediv () {

Getiframe ("Framea"). getElementById ("Ooxx"). Style.backgroundcolor= "#f00"

window.frames["Iframea"].getelementbyid ("Ooxx"). Style.backgroundcolor= "#f00"//Cannot access an element in this form

}

Parent Window Access child window method

function Framefun () {

Getiframe ("Frameb"). Getsfun ()//The subform cannot be accessed by this form a method

window.frames["Iframeb"].getsfun ();

Alert (window.frames["Iframeb"].getsfun ());

}

</script>

</body>

A.html

The code is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>

<title> Untitled Document </title>

<body>

<div id= "Ooxx" > To test the parent form to access an element in the subform </div>

<p id= "Divooxx" > To test a child window B access to a element of form a </p>

<p>1. child window Iframea access to an element of the parent window </p>

<input type= "button" onclick= "Frametopdiv ()" value= "child window accesses an element of the parent window"/>

<input type= "button" onclick= "Frametopfun ()" value= "child window accesses a method of the parent window"/>

<script type= "Text/javascript" >

A 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"

}

child window Access Parent window method

function Frametopfun (ss) {

PARENT.TESTP ("SSSs");

}

Methods for testing IFRAMEB access

function Testba () {

Alert ("Methods for testing Iframeb access")

}

</script>

</body>

B.html

The code is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>

<title> Untitled Document </title>

<body>

<p> Two: Test the subform to access a method or element between them </p>

<input type= "button" Value= Subform b accesses an element of subform a "onclick=" Frametframediv ()/>

<input type= "button" Value= Subform B Access Subform A method "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")//Subform Access Parent form method

_bframe.getelementbyid ("Divooxx"). style.color= "#a0c0f0";

_bframe.getelementbyid ("Divooxx"). style.backgroundcolor= "#000";

}

A method for subform B to access subform A

function Frametframefun () {

window.parent.frames["Framea"].testba ();

}

</script>

<script type= "Text/javascript" >

function Getsfun () {

return "Sssssss";

}

Getfun ()

</script>

</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.