關于document.write為什么會改變body里面的內容?(穿越問)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript">
function a(){
var myweek2=document.getElementById("schedule").value;
switch(parseInt(myweek2)){
case 1:
case 2:
document.write("學習理念知識");
break;
case 3:
case 4:
document.write("到企業實踐");
break;
case 5:
document.write("總結經驗");
break;
case 6:
case 7:
document.write("周六、日休息和娛樂");
break;
default:
document.write("信息出錯了");
}
}
</script>
</head>
<body>
testtest
<label for="schedule">請輸入工作日:</label>
<input type="text" name="plan" id="schedule">
<input type="button" name="button" onclick="a()" value="
點擊我試試">
</body>
</html>
為什么我輸入數字點了按鈕以后, 文本框和我寫在Body里面的test那些字全部都不見了??
如果是這樣, 范例里面不是應該只顯示一行文字嗎?? 感覺 這個問題很白癡,但是我確實不太明白原因在哪里
2016-09-23
打開新頁面時,瀏覽器首先使用document.open()打開一個新文檔,然后將下載的html文本通過document.write()寫入,最后調用document.close()關閉文檔流。
你用button觸發了一個函數,調用document.write()時原文檔已關閉,此時調用document.write()會自動調用document.open()開啟一個新文檔,原文檔內容不會寫入這個新打開的文檔,之后調用的document.write()因為本文檔未關閉,都會被追加輸出到此文檔.加載完成后,自動關閉本文檔.
2016-09-23
因為Document.write的意思就是向頁面輸出一段指定的文本或者內容,而你并沒有給它指定要出現的位置。
2016-09-23
運行會刷新界面