Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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 document.write()重写当前HTML内容。如何避开这个问题?_Javascript_Html - Fatal编程技术网

Javascript document.write()重写当前HTML内容。如何避开这个问题?

Javascript document.write()重写当前HTML内容。如何避开这个问题?,javascript,html,Javascript,Html,我有一个现有的HTML文件,如下所示- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd"> <html> <head> <link rel="stylesheet" type="text/css" href="chatWindow.css" /> <meta http-equ

我有一个现有的HTML文件,如下所示-

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd">
 <html>
 <head>
 <link rel="stylesheet"   type="text/css" href="chatWindow.css" />
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Chat Window</title>
 </head>
 <body>
 <table>
   <tr> <textarea id="chatTextArea"  rows="20" cols = "80"></textarea> </tr>
   <tr> <textarea id="messageTextArea" rows="5"  cols="80"></textarea> </tr>
 </table>
 <script type="text/javascript" src ="jquery-1.7.1.js" ></script>
 <script type="text/javascript" src="liveChat.js"></script>
 </body>
 </html>
现在,在这个现有窗口中,我想添加另一个字段。我试过-

 var chatWindow = window.open("chatWindow.html", "Chat Window", "resizable=0,width=700,height=600");
 chatWindow.document.write(' Hey!!  <input type="hidden" id="currentUserName" value="' + userName+ '"  / >  ');

但这没有影响。当新窗口打开时,我检查了它的页面源,没有找到隐藏的节点。如何解决这个问题?请帮助。

您应该使用appendChild

chatWindow.document.appendChild(document.createTextNode("Hey !"));

您也可以使用jQuery更轻松地完成此操作。

尝试分配属性而不是属性

chatWindow = window.open("chatWindow.html"...);

hiddenNode=chatWindow.document.body.appendChild(chatWindow.document.createElement('INPUT'));
hiddenNode.type='hidden';
hiddenNode.id='currentUserName';
hiddenNode.value=userName;
编辑:

如果浏览器在新选项卡中打开窗口,也可以通过聊天窗口进行访问。
使用Chrome、FF和IE成功测试。

尝试chatWindow.document.body.appendChild/*[…]*/

尝试在加载处理程序中使用.appendChild,例如chatWindow.onload=function{…}。您是否检查了控制台的说明?有什么错误吗?@pimvdb试过了。无法工作。@hotS85控制台未给出任何错误。我检查过了。当你说你检查了“源代码”时,你检查过HTML源代码“查看源代码”还是用Firebug、Web Inspector、Dragonfly…检查过DOM。。?JavaScript不会更改页面的HTML,只是更改DOM。因此,如果你在Firebug、Web Inspector或其他任何工具中“检查元素”,那么隐藏的节点可能就在那里?var mydiv=chatWindow.document.getElementByIdmydiv;mydiv.appendChilddocument.createTextNodebar@蓝色代码我已经编辑了我的答案。我对新标签的看法是错误的。代码经过测试,可以正常工作。
chatWindow.document.appendChild(document.createTextNode("Hey !"));
chatWindow = window.open("chatWindow.html"...);

hiddenNode=chatWindow.document.body.appendChild(chatWindow.document.createElement('INPUT'));
hiddenNode.type='hidden';
hiddenNode.id='currentUserName';
hiddenNode.value=userName;