How to get the contents of the IFRAME frame through JS

Source: Internet
Author: User

Gets the elements in the IFRAME in the parent window

IE under:
Format: window.frames["The name value of the IFRAME"].document.getelementbyid ("ID of the control in the IFrame"). Click (); Example: window.frames[
Methods that are supported:
Format: document.getElementById ("id value of iframe"). ContentWindow.document.getElementsByName (' radios ');//ie Firefox support

gets the element of the parent window in the IFRAME

JS Code

Format: Window.parent.document.getElementById (the element ID of the parent window). Click ();  Example: Window.parent.document.getElementById ("Btnok"). Click ();  

Jquery

Gets the elements in the IFRAME in the parent window

Format: $ ("#iframe的ID"). Contents (). Find ("#iframe中的控件ID"). Click (); // jquery Method 1   Instance: $ ("#ifm"). Contents (). Find ("#btnOk"). Click (); // jquery Method 1   Format: $ ("#iframe中的控件ID", Document.frames ("frame name"). Document). Click (); // jquery Method 2   Instance: $ ("#btnOk", Document.frames ("IFM"). Document). Click (); // jquery Method 2  

Gets the element of the parent window in the IFRAME

Format: $ (' #父窗口中的元素ID ', parent.document). Click ();  Instance: $ ( 

If the page cross-domain, the above method will not work!

Instance code: index.html

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><P>Current frame</P><iframesrc= "if.html"width= "+"Height= "+"ID= "Currentif"name= "Currentif"></iframe></Body></HTML><Scriptlanguage= "JavaScript">window.onload= function(){   varSD=Window.top.document.getElementById ("currentif"). Contentwindow;
You can also get rid of the front window.top here.varson=Sd.document.getElementById ("Linkif"); Son.onclick= function() {alert ("1"); };}
Be sure to put it under the onload. If you write directly, it can cause the element to get failed! </Script>

Instance code: if.html

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><PID= "Linkif">Embedded frame</P></Body></HTML>

How to get the contents of the IFRAME frame through JS

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.