"Go" jquery iframe get elements and adaptive heights

Source: Internet
Author: User

Today is nothing to summarize the IFRAME in jquery How to operate, let me introduce you to jquery to get the IFRAME/parent page elements and IFrame in the jquery highly adaptive implementation method, you can refer to friends.

jquery Method:

Get the parent page element on the IFRAME sub-page

The code is as follows:

1 $ (' #objId ', parent.document);

Gets the element of the IFRAME child page on the parent page
The code is as follows:

1 $ ("#objid", Document.frames (' Iframename '). Document)23 $ ( document.getElementById (' Iframeid '). contentWindow.document.body). HTML ()

Displays the contents of the BODY element in the IFRAME.

1 $ ("#testId", Document.frames ("Iframename"). Document). HTML ();

The element with the id "TestID" is obtained according to Iframename.

$ (window.frames["Iframename"].document). Find ("#testId"). HTML ()

In the parent window, manipulate all the input boxes in the selected IFRAME:

$ (window.frames["Iframeson"].document). Find (": Text");

Action in the IFRAME Select all the input boxes in the parent window:

$ (window.parent.document). Find (": Text");

The HTML of the IFRAME framework:

<src= "test.html"  ID= "Iframeson"  width= "700″ height= "300″frameborder=" 0″scrolling= "Auto"></iframe  >

Manipulate all radio buttons in the selected IFrame in the parent window.

$ (window.frames["iframe1"].document). Find ("input[@type = ' Radio ']"). attr ("Checked", "true");

Action in the IFRAME selects all the radio buttons in the parent window.

$ (window.parent.document). Find ("input[@type = ' Radio ']"). attr ("Checked", "true");

Of the IFRAME framework:

<src= "test.html"  ID= "iframe1″width="700″height = "300″frameborder=" 0″scrolling = "Auto" ></ iframe >

Suppose there are two pages under the same domain.

The index.html file contains an iframe:

xml/html Code

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//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>Page Home</title> </Head>  <Body> <iframesrc= "iframe.html"ID= "Koyoz"Height= "0"width= "0"></iframe> </Body> </HTML>  

Iframe.html content:

xml/html Code

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//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>Iframe.html</title> </Head>  <Body> <DivID= "Test">Www.</Div> </Body> </HTML> 

IFRAME Adaptive Height:

$ ("#iPersonalInfo"). Load (function() {$ (this). Height ($ (this). Contents (). Height ());})

There is a point to note, I also in the commissioning of the time only to find, delayed a lot of time. The binding event must be bound before the IFrame has finished loading, otherwise it will not execute.
The following is an overview of the Jquery,load event
Binds a handler function in each of the load events of the matching element.
If bound to a Window object, it fires after all content is loaded, including Windows, frames, objects, and images. If it is bound to an element, it is triggered when the content of the element has finished loading.
Note: A handler function that binds load before the element is fully loaded will be triggered when he finishes loading. If you do, then the binding will never be triggered. So do not bind the Load event in $ (document). Ready () because jquery binds the Load event after all DOM loading is complete.

IFRAME code, note to write ID

<src= "test.html"  ID= "main"  width= " the" Height = "+" frameborder = "0" scrolling = "Auto" ></ iframe >

jquery Code 1:

// Note: The following code is placed on Test.html call $ (window.parent.document). Find ("#main"). Load (function() {  var main = $ (window.parent.document). Find ("#main"); var thisheight = $ (document). Height () +30; Main.height (thisheight);});

jquery Code 2:

// Note: The following code is placed on the same page as the IFRAME called $ ("#main"). Load (function() {var mainheight = $ (  this). Contents (). Find ("body"). Height () +30; $ (this). Height (mainheight);});

"Go" jquery iframe get elements and adaptive heights

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.