javascript 如何将数据从父 html 窗口传递到弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17559050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:46:32  来源:igfitidea点击:

How to pass data from Parent html window to pop-up window

javascripthtmlpopup

提问by arsenal

I am trying to pass some data from parent window to pop up window in html.

我试图将一些数据从父窗口传递到 html 中的弹出窗口。

Below is my code-

下面是我的代码-

<html>
<head>
<script type="text/javascript">
function init()
{
    popupWin = window.open('','popupWin','');
    popupWin.document.writeln('<html><head><title>test</title></head><body><form><input type="text" id="popupTextBox"/></form></body></html>');
    popupWin.document.close();
    popupText = popupWin.document.getElementById("popupTextBox");
    parentText = document.getElementById("parentTextBox");
}
function transferText()
{
    popupText.value = parentText.value
}
</script>
</head>
<body>
<input type="text" id="parentTextBox"/>
<input type="button" onclick="init();"/>
</body>
</html>

But somehow I am not able to pass that textbox data to popup window with the above code. Is there any problem with this?

但不知何故,我无法使用上述代码将该文本框数据传递给弹出窗口。这有什么问题吗?

In general, I am trying to pass some data from parent window to popup window.

一般来说,我试图将一些数据从父窗口传递到弹出窗口。

回答by Fabian N.

You forgot to call transferText()
After calling transferText()the text was transferred...

你忘记打电话了 来电transferText()
transferText()短信被转...

<html>
<head>
<script type="text/javascript">
function init()
{
    popupWin = window.open('','popupWin','');
    popupWin.document.writeln('<html><head><title>test</title></head><body><form><input type="text" id="popupTextBox"/></form></body></html>');
    popupWin.document.close();
    popupText = popupWin.document.getElementById("popupTextBox");
    parentText = document.getElementById("parentTextBox");
    transferText();
}
function transferText()
{
    popupText.value = parentText.value
}
</script>
</head>
<body>
<input type="text" id="parentTextBox"/>
<input type="button" onclick="init();"/>
</body>
</html>