这是一个来自mozilla taiwan论坛里的问题,这种情况很多人也应该是遇到过的。。。只是或许都采用了一些绕过的方法来解决的吧?
问题如下:
請問一下以下的html code有什麼問題嗎?
在IE下很正常,
但在Firefox裏第二行的document.write好像就不能運作了
而且一直顯示"載入中",好像在什麼地方卡住了..
原始碼如下:
------------------
<html>
<head>
<title> new document </title>
<script type="text/javascript">
<!--
var tmp = new Array(2);
tmp[0] = new Array(2);
tmp[0][0] = "lalala";
tmp[0][1] = "hahaha";
function test(){
document.write(tmp[0][0]);
document.write(tmp[0][1]);
}
//-->
</script>
</head>
<body>
<input type="button" value="test" onclick="javascript:test();">
</body>
</html>
但,事实是什么样的的?有人这样回复:
document.write 表示你要重寫整個頁面,所以第一個指令執行後,整個頁面已予清除,第二個指令也就不見了
另外,JS 語法也不正確,應該是
document.open();
document.write(...);
:
document.close();
如果不要清除網頁,可用:
text = document.createTextNode("blah blah blah");
document.body.appendChild(text);
最终,这样就解决了:
<html>
<head>
<title> new document </title>
<script type="text/javascript">
<!--
var tmp = new Array(2);
tmp[0] = new Array(2);
tmp[0][0] = "lalala";
tmp[0][1] = "hahaha";
function test(tmp2){
document.open();
document.write(tmp2[0][0]);
document.write(tmp2[0][1]);
document.close();
}
//-->
</script>
</head>
<body>
<input type="button" value="test" onclick="javascript:test(tmp);">
</body>
</html>

评论 (1)
赶上架的鸭子!