jquery, JS call the method of the iframe parent window and child window elements

Source: Internet
Author: User

1. jquery Gets the parent page element code in the IFRAME sub-page as follows:

$ ("#objid", parent.document)

2. jquery Gets the elements of the IFRAME child page on the parent page

The code is as follows:

$ ("#objid", Document.frames (' Iframename '). Document)

3.js Gets the parent page element code in the IFRAME sub-page as follows:

Indow.parent.document.getElementByIdx_x ("Element id");

4.js gets the IFRAME child page element code on the parent page as follows:

window.frames["iframe_id"].document.getelementbyidx_x ("element ID");

5. Call the parent class function inside the subclass IFRAME:

Window.parent.func ();

Using the native JS on the parent page to get the elements of the IFRAME sub-page, as well as the child page to get the parent page elements, which is usually used in the method, here is an example to summarize the following:

1. Parent page (demo.html), modify sub-page in parent page the background color of the div is gray and the original is red:

[HTML]View PlainCopy
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
  5. <title>demo Main Page </title>
  6. <script type="Text/javascript">
  7. window.onload = function () {
  8. var _iframe = document.getElementById (' Iframeid '). Contentwindow;
  9. var _div =_iframe.document.getelementbyid (' objId ');
  10. _div.style.backgroundcolor = ' #ccc ';
  11. }
  12. </Script>
  13. </head>
  14. <body>
  15. <div id=' Pardiv '> Parent page </div>
  16. <iframe src="demo-iframe.html" id="Iframeid" height= "Width=" ></iframe>
  17. </body>
  18. </html>


2, sub-page (demo-iframe.html), in the child page to modify the parent page div font color is red, originally black:

[HTML]View PlainCopy
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
  5. <title> sub-page demo13-iframe.html</title>
  6. <script type="Text/javascript">
  7. window.onload = function () {
  8. var _iframe = window.parent;
  9. var _div =_iframe.document.getelementbyid (' Pardiv ');
  10. _div.style.color = ' red ';
  11. }
  12. </Script>
  13. </head>
  14. <body>
  15. <div id= 'objId ' style=' width:100px;height:100px;background-color:red; ' > Sub page </div>
  16. </body>
  17. </html>


3.:

(1) When JS is not added:


(2) after adding JS:

jquery, JS call the method of the iframe parent window and child window elements

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.