在函數里添加document.write,為什么不像之前的例子在html后面追加輸出呢?
<!DOCTYPE HTML>
<html>
? ? <head>
? ? ? ? <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
? ? ? ? <title>無標題文檔</title>
? ? </head>
? ??
? ? <body>
? ? ? ? <form>
? ? ? ? ? 請選擇你愛好:<br>
? ? ? ? ? <input type="checkbox" name="hobby" id="hobby1" value="music">? 音樂
? ? ? ? ? <input type="checkbox" name="hobby" id="hobby2" value="music">? 登山
? ? ? ? ? <input type="checkbox" name="hobby" id="hobby3" value="music">? 游泳
? ? ? ? ? <input type="checkbox" name="hobby" id="hobby4" value="music">? 閱讀
? ? ? ? ? <input type="checkbox" name="hobby" id="hobby5" value="music">? 打球
? ? ? ? ? <input type="checkbox" name="hobby" id="hobby6" value="music">? 跑步 <br>
? ? ? ? ? <input type="button" value = "全選" onclick = "checkall()">
? ? ? ? ? <input type="button" value = "全不選" onclick = "clearall();">
? ? ? ? ? <p>請輸入您要選擇愛好的序號,序號為1-6:</p>
? ? ? ? ? <input id="wb" name="wb" type="text" >
? ? ? ? ? <input name="ok" type="button" value="確定" onclick = "checkone();">
? ? ? ? </form>
? ? ? ? <script type="text/javascript">
? ? ? ? function checkall(){
? ? ? ? ? ? var hobby = document.getElementsByTagName("input");
? ? ? ? ? ? for(i=0;i<hobby.length;i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(hobby[i].type=="checkbox")
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //document.write(hobby[i].value+"<br>");? ?//為什么這一行取消注釋,運行的話,點擊全選過后,只有一個music的值輸出呢???
? ? ? ? ? ? ? ? ? ? hobby[i].checked=true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? // 任務1? ? ??
? ? ? ? }
? ? ? ? function clearall(){
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? ?for(i=0;i<hobby.length;i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(hobby[i].type=="checkbox")
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? hobby[i].checked=false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ?// 任務2? ? ??
? ? ? ? }
? ? ? ? function checkone(){
? ? ? ? ? ? var j=document.getElementById("wb").value;
? ? ? ? ? ? var j_id="hobby"+j;
? ? ? ? ? ? document.getElementById(j_id).checked=true;
? ? ? ? ?// 任務3
? ? ? ? }
? ? ? ? </script>
? ? </body>
</html>
2019-06-23
我覺得頁面加載時先輸出html內容
然后解析js代碼 如果有輸出流(document.write())那么就輸出確實能正常輸出
沒有的話解析完?
onload事件完成后再觸發別的事件(例如onClick)然后執行該事件 里面有輸出流(document.write())的話就會覆蓋原界面吧
2019-06-23
document.write()一般用于頁面 onload 的時候。如果頁面已經 onload 完了,也就是頁面加載完成了,再調用docume.write()的話,那么,整個 HTML 頁面將被覆蓋
轉自:https://www.cnblogs.com/holy-night/articles/5122860.html