MozTW 討論區
https://forum.moztw.org/

[問題]javascript中的document.write
https://forum.moztw.org/viewtopic.php?f=13&t=8606
1 頁 (共 1 頁)

發表人:  havohej [ 2005-05-26, 19:13 ]
文章主題 :  [問題]javascript中的document.write

請問一下以下的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>

發表人:  danielwang [ 2005-05-26, 19:20 ]
文章主題 : 

document.write 表示你要重寫整個頁面,所以第一個指令執行後,整個頁面已予清除,第二個指令也就不見了

另外,JS 語法也不正確,應該是
document.open();
document.write(...);
:
document.close();

如果不要清除網頁,可用:
text = document.createTextNode("blah blah blah");
document.body.appendChild(text);

發表人:  訪客 [ 2005-05-26, 20:18 ]
文章主題 : 

更新後html code,可以執行了,感謝 :lol:
--------------------------------------
<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("lalala");
document.write("hahaha");
document.close();
}
//-->
</script>
</head>
<body>&nbsp;
<input type="button" value="test" onclick="javascript:test(tmp);">
</body>
</html>

發表人:  havohej [ 2005-05-26, 20:22 ]
文章主題 : 

上一篇寫錯.,用guest不能修改...:cry:,更正如下..
--------------------------
<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 頁 (共 1 頁) 所有顯示的時間為 UTC + 8 小時
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/