Home > Article > Web Front-end > Detailed explanation of common methods of jQuery to obtain elements in iframe_jquery
This article analyzes the common methods of jQuery to obtain elements in iframe. Share it with everyone for your reference, the details are as follows:
Several ways to get elements in iframe with jquery:
Get the parent page element in the iframe subpage
The code is as follows:
Done...
Get the elements of the iframe subpage in the parent page:
$("#objid",document.frames('iframename').document) $(document.getElementById('iframeId').contentWindow.document.body).html()
Display the content of the body element in the iframe.
Get the element whose ID is "testId" based on iframename
Use JS or jQuery to access the iframe in the page, compatible with IE/FF
Note: Pages within the frame cannot cross domains!
Suppose there are two pages, under the same domain.
Theindex.html file contains an iframe:
<!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>页面首页</title> </head> <body> <iframe src="iframe.html" id="koyoz" height="0" width="0"></iframe> </body> </html>
iframe.html content:
<!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.jb51.net</div> </body> </html>
1. Execute JS in index.html for direct access:
This code has been tested and can support IE/firefox.
2. Access with jQuery in index.html:
The effect of this code is the same as direct access through JS. With the help of the jQuery framework, the code is shorter.
Collect some examples from the Internet:
Using jQuery to obtain the value of an element of the parent window in the IFRAME can only be achieved by combining the DOM method and the jquery method
1. Operate in the parent window and select all radio buttons in the IFRAME
I hope this article will be helpful to everyone in jQuery programming.