JS gets the element in the IFRAME and the element that gets the parent in the IFRAME (including the case where the name and ID do not exist in the IFRAME)

Source: Internet
Author: User

First case: The method of name and ID does not exist in the IFRAME: (obtained by Contentwindow) var iframe = document.getElementsByTagName (' iframe ') [0];
var ifr_document = The contents of the document in Iframe.contentwindow.document;//iframe or:var _iframe = document.getelementbyidx_x (' Iframeid '). Contentwindow; var _div =_iframe.document.getelementbyidx_x (' objId ');
or:
    1. var framewin=document.getElementById (' iframe '). Contentwindow; Window Object
    2. var framedoc=document.getElementById (' Iframeid '). Contentwindow.document//document Object
    3. var framebody=document.getElementById (' Iframeid '). ContentWindow.document.body//body to
Second case: The method of name or ID in the IFRAME: (obtained via frames[] array) document.frames[' iframe name '].document.getelementbyid (' element id '); Third case: Get the ID element of the parent page in the IFRAME: Varobj=window.parent.document.getElementById (' objId ');
$ (window.parent.document). Find ("#objId"). CSS (' height ': ' height '); Window can omit not to write jqueryFourth case: Parent forms accessing properties in an IFRAME
    1. A, using the Contentwindow method
    2. document.getElementById (' iframe1 '). onload=function () {
    3. This.contentWindow.run ();
    4. }
    5. b, using iframes[] Frameset Array method
    6. document.getElementById (' iframe1 '). onload=function () {
    7. frames["Iframe1"].run ();
    8. }
Fifth case: Methods and properties that access the parent form in the IFRAME//window can not write
    1. Window.parent.attributeName; Access attribute AttributeName is the property name in the parent window
    2. Window.parent.Func (); Access property func () is a method in the parent window
Fifth case:Make IFRAME Adaptive height
    1. $ (' #iframeId '). Load (function ()  { //method 1  
    2.     var iframeheight =  math.min (Iframe.contentWindow.window.document.documentElement.scrollHeight,  iframe.contentwindow.window.document.body.scrollheight);   
    3. h=$ (this). Contents (). Height ();   
    4.     $ (this). Height (H + ' px ');    
    5. });  
    6.   
    7. $ (' #iframeId '). Load (function ()  { //method 2  
    8.     var iframeheight=$ (this). Contents (). Height ();   
    9.     $ (this). Height (iframeheight+ ' px ');   
    10. });  

JS Gets the element in the IFRAME and the element that gets the parent in the IFRAME (including when the name and ID do not exist in the IFRAME)

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.