The parent and child iframe get the concrete implementation of the variable and element object from each other

  • 2020-03-26 21:30:47
  • OfStack

Father:
 
<input id="username"type="text" /> 
<scripttype="text/javascript"> 
var count = 1; 
</script> 

-------------------------------------------------------------
Child iframe:
 
<scripttype="text/javascript"> 
alert(window.parent.count); //Gets the js variable in the parent
alert($("#username", window.parent.document));//Gets the jQuery object in the parent
alert(window.parent.document.getElementByIdx_x('username'));//Gets the DOM object in the parent
</script> 

--------------------------------------------------------------------------------------------------

Child iframe (id="iframeId"name="iframeName") :
 
<input id="username"type="text" /> 
<scripttype="text/javascript"> 
varcount = 1; 
</script> 

-------------------------------------------------------------
Father:
 
<scripttype="text/javascript"> 
alert(document.iframeName.count);//Gets the js variable in the child iframe
alert($(window.frames["iframeName"].document).contents().find("#username"));//Gets the jQuery object in the child iframe
alert($("#username",document.frames("iframeName").document));//Gets the jQuery object in the child iframe
alert(window.document.getElementById('iframeId').contentWindow.document.getElementByIdx_x('username'));//Gets the DOM object in the child iframe
</script> 

Collect some examples online:
Get the value of an element of the parent window in the IFRAME with jQuery
DOM method and jquery method to combine the way to achieve

1. In the parent window, select all radio buttons in the IFRAME
$(window frames [r]. "iframe1" document), find (" input: radio "). Attr (" checked ", "true");
2. Operate all the radio buttons in the selected parent window in the IFRAME
$(window. The parent. The document). The find (" input: radio "). Attr (" checked ", "true");
The parent window wants to get the IFrame in the IFrame, just add another frames level, such as:
$(window frames [r]. "iframe1" frames [r]. "iframe2" document), find (" input: radio "). Attr (" checked ", "true");
3. Call the method of another child window in the parent window in the child window (FRAME) :
Parent. Frames [r]. "Main" Fun ();
Note: suggest to use [], so more compatible with multiple browsers, () firefox/sogou/Google is not compatible.

Related articles: