document.write()的;删除其他HTML [英] document.write(); removes other HTML

查看:69
本文介绍了document.write()的;删除其他HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个评论的测试。我只想要一个小文本框,在其中键入内容和一个表示添加注释的按钮,将document.write();在添加注释事件下的文本框中放入了什么。但我遇到了一个问题,其中document.write();似乎正在删除所有其他写在javascript边的HTML(即textarea和添加评论按钮)。当我按下添加评论按钮时,我在文本区写的内容填满了整个屏幕,似乎正在弄脏其余部分。这里是我的代码:

 < html> 
< head>
< script language =JavaScript>
函数add1(){
var tf = document.getElementById('tf');
add2(tf.value);
}
< / script>
< / head>
< body>
< p>在此输入stuffz:< / p>
< textarea id =tfwrap =logicalrows =10cols =50>< / textarea>
<! - < input type =textfielid =tfvalue =Test> - >
< br>
< input type =buttononClick =add1()value =添加评论>

< script type =text / javascript>
function add2(input){
document.writeln(input);
}
< / script>
< / body>
< / html>


解决方案

您不能使用文档.write文件完成加载后,写入。如果你这样做,那么浏览器将打开一个新的文档,并将其替换为当前的文档。因此,这是 document.write

的设计行为。

使用







$ b 尝试像这样:

< HTML>
< head>
< script language =JavaScript>
函数add1(){
var tf = document.getElementById('tf');
add2(tf.value);
}
< / script>
< / head>
< body>
< p id =test>输入stuffz here:< / p>
< textarea id =tfwrap =logicalrows =10cols =50>< / textarea>
<! - < input type =textfielid =tfvalue =Test> - >
< br>
< input type =buttononClick =add1()value =添加评论>

< script type =text / javascript>
函数add2(输入){

var test = document.getElementById('test');
test.innerHTML = input;
}
< / script>
< / body>
< / html>

同时检查为什么document.write被认为是坏习惯?


I'm doing a test for a comment thing. All I want is to have a little text box where you type stuff and a button that says "Add Comment" that will document.write(); what you put in the text box under the add comment thing. But I'm getting a problem where document.write(); seems to be removing all the other HTML that was written out side the javascript (i.e. the textarea and the "Add Comment" button). When I press the "Add Comment" button, what I wrote in the textarea fills up the whole screen and seems to be blotching out the rest. Here is my code:

<html>
<head>
<script language="JavaScript">
  function add1(){
   var tf = document.getElementById('tf');
   add2(tf.value);
  }
 </script>
</head>
<body>
<p>Type stuffz here:</p>
<textarea id="tf" wrap="logical" rows="10" cols="50"></textarea>
<!--<input type="textfiel" id="tf"  value="Test">-->
<br>
<input type="button" onClick="add1()" value="Add Comment" >

<script type = "text/javascript">
function add2(input){
    document.writeln(input);
}
</script>
</body>
</html>

解决方案

You can not use document.write once the document has completed loading. If you do that then browser will open a new document and it will replace it with the current. So it is the design behavior of document.write

It would be better to use innerHTML to put HTML inside element

Try like this:

<html>
<head>
<script language="JavaScript">
  function add1(){
   var tf = document.getElementById('tf');
   add2(tf.value);
  }
 </script>
</head>
<body>
<p id="test">Type stuffz here:</p>
<textarea id="tf" wrap="logical" rows="10" cols="50"></textarea>
<!--<input type="textfiel" id="tf"  value="Test">-->
<br>
<input type="button" onClick="add1()" value="Add Comment" >

<script type = "text/javascript">
function add2(input){

    var test = document.getElementById('test');
    test.innerHTML = input;
}
</script>
</body>
</html>

Also check Why is document.write considered a "bad practice"?

这篇关于document.write()的;删除其他HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆