Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript在新窗口的html字段中显示值?_Javascript_Html - Fatal编程技术网

如何使用javascript在新窗口的html字段中显示值?

如何使用javascript在新窗口的html字段中显示值?,javascript,html,Javascript,Html,我基本上是尝试验证一个输入字段,打开一个新窗口,并在新窗口中显示输入字段的内容。我已打开新窗口,但不会将文本发布到“displayText”字段 html: 第三季度 文本: 提交 javascript: function validateQ3() { // Get value from text box var a = document.getElementById("tb5").value; // Check that textbox is neither

我基本上是尝试验证一个输入字段,打开一个新窗口,并在新窗口中显示输入字段的内容。我已打开新窗口,但不会将文本发布到“displayText”字段

html:


第三季度
文本:
提交
javascript:

function validateQ3() {
    // Get value from text box
    var a = document.getElementById("tb5").value;
        // Check that textbox is neither empty nor contains null
    if (a == null || a == "")
    {
        // if error, display error
        alert("Textbox cannot be empty");
        return false;
    }
    // Check text field >= 5
    if (a.length < 5)
    {
        alert("Field must be longer than 5 characters");
        return false;
    }
    else 
    {
        // Call newWindow() function
        newWindow(a);
    }
}//validateQ3()

function newWindow(a) {
    // Open new window
    var newWindow = window.open("", "mypopup", "height=300 , width=300");
    newWindow.document.write("<p id='displayText'></p>");
    if (window.focus)
    {
        newWindow.focus();
    }
    var tmp = newWindow.document;
    tmp.write('<html><head><title>mypopup</title></head>');
    tmp.write('<body><p id="displayText"></p></body></html>');
    document.getElementById("displayText").innerHTML = a;
    tmp.close();
}
函数validateQ3(){
//从文本框中获取值
var a=document.getElementById(“tb5”).value;
//检查文本框既不为空也不包含null
如果(a==null | | a==“”)
{
//如果出现错误,则显示错误
警报(“文本框不能为空”);
返回false;
}
//检查文本字段>=5
如果(a.长度<5)
{
警报(“字段必须超过5个字符”);
返回false;
}
其他的
{
//调用newWindow()函数
新窗口(a);
}
}//validateQ3()
函数newWindow(a){
//打开新窗口
var newWindow=window.open(“,“mypopup”,“高度=300,宽度=300”);
newWindow.document.write(“

”; if(window.focus) { newWindow.focus(); } var tmp=newWindow.document; tmp.write('mypopup'); tmp.write('

'); document.getElementById(“displayText”).innerHTML=a; tmp.close(); }

任何帮助

如果您试图在当前窗口而不是新窗口上设置元素的innerHTML,第一个窗口上不存在displayText,请将文档更改为:

 tmp.getElementById("displayText").innerHTML = a;

问题可能是调用
document.getElementById
而不是
tmp.getElementById
<代码>文档指的是
窗口。文档
是当前窗口,而不是您打开的新窗口。您可能会找到错误所在:感谢Greg花时间回答。这也是问题所在,如果不需要
a
进行HTML解析,请使用
textContent
,而不是
innerHTML
。谢谢,这就解决了问题。我知道这和窗户有关,但找不到答案
 tmp.getElementById("displayText").innerHTML = a;