日期:2014-05-16  浏览次数:20348 次

关于<pre>标签的使用
HTML code
 
<body>
<div id="t" style="border:1px solid red;width:200px;height:300px;"> </div>
<textarea onkeydown="fun1(this)"> </textarea>
<textarea onkeydown="fun2(this)"> </textarea>
</body>
<SCRIPT LANGUAGE="JavaScript">
<!--
function fun1(obj)
{
document.getElementById("t").innerHTML=" <pre>"+obj.value+" </pre>";
}

function fun2(obj)
{document.getElementById("t").innerHTML=""
var p=document.createElement("pre");
document.getElementById("t").appendChild(p);
p.innerHTML=obj.value;
}
//-->
</SCRIPT>


------解决方案--------------------

<body>
<div id="t" style="border:1px solid red;width:200px;height:300px;"></div>
<textarea onkeydown="fun1(this)"></textarea>
<textarea onkeydown="fun2(this)"></textarea>
</body>
<SCRIPT LANGUAGE="JavaScript">
<!--
function fun1(obj)
{
document.getElementById("t").innerHTML="<pre>"+obj.value+"</pre>";
}

function fun2(obj)
{document.getElementById("t").innerHTML=""
var p=document.createElement("pre");
var text = document.createTextNode(obj.value);
p.appendChild(text);
document.getElementById("t").appendChild(p);
}
//-->
</SCRIPT>

这样写比较正规吧,经测试有效,你的代码只有在IE下不可以吧,其它的浏览器我测试过,都没有问题.