Home  >  Article  >  Web Front-end  >  Basic usage of objects calling each other between parent and child windows of iframe_javascript skills

Basic usage of objects calling each other between parent and child windows of iframe_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:23:431116browse

When using an iframe, it may involve passing values ​​and calling methods between parent and child windows. I was a little confused before, and I didn't bother to figure it out. I have to work these two days, and I have no choice but to figure it all out. . It's actually very simple, just a few usages and a few lines of code.

Call the js method of the parent window in the child window:

Copy the code The code is as follows:

parent.changeBtnTitle('tzgg');

A parent window may have several child windows with similar functions and behaviors. The components in the child windows need to call the same or similar objects. Or method, then write such a method in the parent window and achieve different effects by passing parameters.

The parent window gets an object in the child window:

First get the iframe object of the child window, for example:
Copy Code The code is as follows:

var frameName = window.frames["frameId"]

Then you want to get the document of the sub-window value, and then you can call the object in the child window. In order to avoid repeated writing when using it in the parent window, you might as well write a method:
Copy the code The code is as follows:

getSubWindowObj = function(moduleName){
return window.frames["iframe" moduleName].document.getElementById("moreorless" moduleName);
}

Here only I used a parameter just because my iframeId and the object ID of the sub-window were named regularly. I just need to pass in a parameter and splice it together.

Similarly, when a child page calls an element of a parent page, you can use the following code:
Copy the code The code is as follows:

parent.window.document.getElementById('parentMenu');
//Abbreviation
parent.document.getElementById('parentMenu');
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn