This article mainly introduces the implementation code for js to determine whether an element exists in iframe. If you need it, refer to this article to introduce JavaScript to determine whether the element in iframe exists in code, for more information, see.
I. Pure Original Ecological js implementation method, the Code is as follows:
Script var bb = extends ument. getElementById ('apic '); if (bb) {}else {}// aPic is the Id of the element in the Child Page Preview. aspx script
II. The popular jquery implementation method is as follows:
if($(window.frames["iframepage"].document).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
$ ('# ObjId', parent.doc ument); // get the elements of the iframe subpage on the parent page $ ("# objid" contents ()
Display the content of the body element in iframe
$ ("# TestId", document. frames ("iframename" 2.16.document).html (); get the "testId" element $ (window. frames ["iframeName" 2.16.doc ument ). find ("# testId" pai.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:
Homepage
Iframe.html content:
iframe.htmlwww.php1.cn
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:
$ ("# 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.
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 ")
For more articles about js implementation code used to determine whether an element exists in iframe, refer to PHP!