Home >Web Front-end >JS Tutorial >js and jQuery get iframe_javascript skills of parent window and child window
In web development, iframes are often used. It is inevitable that you need to use elements in the iframe in the parent window, or use elements of the parent window in the iframe
js
Get elements in iframe in parent window
1,
Format: window.frames["name value of iframe"].document.getElementByIdx_x("ID of control in iframe").click();
Example: window.frames["ifm"].document.getElementByIdx_x("btnOk").click();
2,
Format:
var obj=document.getElementByIdx_x("name of iframe").contentWindow;
var ifmObj=obj.document.getElementByIdx_x("ID of the control in the iframe");
ifmObj.click();
Example:
var obj=document.getElementByIdx_x("ifm").contentWindow;
var ifmObj=obj.document.getElementByIdx_x("btnOk");
ifmObj.click();
Get the elements of the parent window in the iframe
Format: window.parent.document.getElementByIdx_x("Element ID of parent window").click();
Example: window.parent.document.getElementByIdx_x("btnOk").click();
jquery
Get elements in iframe in parent window
1,
Format: $("#iframe's ID").contents().find("#iframe's control ID").click();//jquery method 1
Example: $("#ifm").contents().find("#btnOk").click();//jquery method 1
2,
Format: $("#Control ID in iframe",document.frames("name of frame").document).click();//jquery method 2
Example: $("#btnOk",document.frames("ifm").document).click();//jquery method 2
Get the elements of the parent window in the iframe
Format: $('#Element ID in the parent window', parent.document).click();
Example: $('#btnOk', parent.document).click();