Example
The code is as follows: |
Copy code |
If ($ (window. frames ["iframepage" 2.16.doc ument). find ('. l-grid-row-cell'). length> 0 ){ Alert (1 ); } Else { Alert (2 ); }
|
The preceding code checks whether the element of css 1-grid-row-cell exists in iframe with the id of iframepage.
Appendix: several methods for Jquery to obtain elements in iframe
Obtain the elements of the parent page on the iframe subpage
The code is as follows: |
Copy code |
$ ('# ObjId', parent.doc ument ); // Get it done... Obtain the element of the iframe subpage on the parent page. $ ("# Objid", document.frames('iframename'assist.doc ument) Certificate (document.getelementbyid('iframeid'}.content?#document.body=.html ()
|
Displays the content of the body element in iframe.
The code is as follows: |
Copy code |
$ ("# 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 ()
|
2. Access the iframe in the page with JS or jQuery, 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:
The code is as follows: |
Copy 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:
The code is as follows: |
Copy 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.111cn.net </div> </Body> </Html> 1. Execute JS access in index.html: Document.getelementbyid('koyoz'{.content}doc ument. getElementById ('test'). style. color = 'red'
|
Access the iframe page with the ID 'koyoz' in index.html, obtain the object with the ID of 'Test' in the iframe page, and set its color to red.
This code has been tested and supports IE/firefox.
2. Access through jQuery in index.html:
The code is as follows: |
Copy 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.
In addition, some netizens provide the following examples:
JQuery is used to obtain the value of an element in the parent window in IFRAME, so we have to combine the DOM method with the jquery method.
The code is as follows: |
Copy code |
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 ") |