Several Methods for jquery to obtain elements in IFRAME JavaScript jquery to obtain the element, content, or ID of IFRAME

Source: Internet
Author: User

 

Jquery gets several methods of IFRAME elements. Javascript jquery gets the element, content, or ID of IFRAME, and vice versa!

Several methods for obtaining elements in IFRAME through query

 

Obtain the elements of the parent page on the IFRAME subpage

The Code is as follows:


$ ('# Objid', parent.doc ument );

// Get it done...



Obtain the element of the IFRAME subpage on the parent page.

The Code is as follows:


$ ("# Objid", document.frames('iframename'assist.doc ument)

 

Certificate (document.getelementbyid('iframeid'}.content?#document.body=.html ()
 
Displays the content of the Body element in IFRAME.

 
$ ("# Testid", document. Frames ("iframename" 2.16.document).html ();

Obtain the "testid" element based on iframename.

$ (Window. Frames ["iframename" cmd.doc ument). Find ("# testid" cmd.html ()

 

Use JS or jquery to access IFRAME on the page, and be compatible with IE/FF
Note: pages in the framework cannot be cross-origin!

Assume there are two pages in 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>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> homepage </title>
</Head>

<Body>
<IFRAME src = "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>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> iframe.html </title>
</Head>

<Body>
<Div id = "test"> www.koyoz.com </div>
</Body>
</Html>

1. Execute JS access in index.html:

JavaScript code
Document.getelementbyid('koyoz'{.content}w.doc ument. getelementbyid

('Test'). style. Color = 'red'
Access the IFRAME page with the ID 'koyoz' in index.html and obtain

Object, and set its color to red.

This code has been tested and supports IE/Firefox.

2. Access through jquery in index.html:

JavaScript code
$ ("# Koyoz"). Contents (). Find ("# test" ).css ('color', 'red ');
The effect of this Code is the same as that of direct access to JS. Because of the jquery framework, the code is shorter.
 

Some examples of online collection:
Use jquery to obtain the value of an element in the parent window in IFRAME.
We had to use the DOM method and jquery method to achieve this.

1. In the parent window, select all radio buttons in IFRAME.
$ (Window. Frames ["iframe1" cmd.doc ument). Find ("input: Radio"). ATTR ("checked", "true ");

2. in IFRAME, select all radio buttons in the parent window
Vertex (window.parent.doc ument). Find ("input: Radio"). ATTR ("checked", "true ");

If you want to obtain the IFRAME In the IFRAME in the parent window, you can add another frames sub-level. For example:
$ (Window. Frames ["iframe1"]. Frames ["iframe2" ].doc ument). Find ("input: Radio"). ATTR ("checked", "true ");

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.