Using jquery and js to get iframe parent child level and sibling to get elements

  • 2021-07-09 06:48:18
  • OfStack

In the development of web, iframe is often used, and it is inevitable that the elements in iframe need to be used in the parent window, or the elements in the parent window need to be used in the iframe framework

js

Getting Elements in iframe in Parent Window

1.

Format: window. frames ["name value for iframe"]. document. getElementById ("ID for controls in iframe"). click ();

Examples: window. frames ["ifm"]. document. getElementById ("btnOk"). click ();

2.

Format:


var obj=document.getElementById("iframe Adj. name").contentWindow;
var ifmObj=obj.document.getElementById("iframe Control in the ID");
ifmObj.click();

Example:


var obj=document.getElementById("ifm").contentWindow;

var ifmObj=obj.document.getElementById("btnOk");

ifmObj.click();

Get the element of the parent window in iframe

Format: window. parent. document. getElementById ("Element ID of Parent Window"). click ();

Examples: window. parent. document. getElementById ("btnOk"). click ();

jquery

Getting Elements in iframe in Parent Window

1.

Format: $("ID of # iframe"). contents (). find ("ID in # iframe"). click (); //jquery Method 1

Example: $("# ifm"). contents (). find ("# btnOk"). click (); //jquery Method 1

2.

Format: $("ID in # iframe", document. frames ("name in frame"). document). click (); //jquery Method 2

Example: $("# btnOk", document. frames ("ifm"). document). click (); //jquery Method 2

Get the element of the parent window in iframe

Format: $('# Elements in Parent Window ID', parent. document). click ();

Example: $('# btnOk', parent. document). click ();


Related articles: