如何使用javascript在新窗口的html字段中显示值?
我基本上是尝试验证一个输入字段,打开一个新窗口,并在新窗口中显示输入字段的内容。我已打开新窗口,但不会将文本发布到“displayText”字段 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
第三季度
文本:
提交
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;