最近一直在研究JavaScript中的DOM(文件物件模型),當然,不能說是研究了,頂多也就是一個學習吧,至於DOM的邏輯文法倒並不是很難的,只是自己在編碼是難免要出現一些小的錯誤,這裡不好用誤差一詞。
引入錯誤之前先比較兩段代碼,看看有什麼區別沒有:
1》
window.onload=initAll;
function
initAll(){
document.getElementById('form1').onsubmit=function(){return
addNode();}
//document.getElementById('delNode').onclick=delNode;
}
function
addNode(){
var inText=document.getElementById('textArea').value;
var
newText=document.createTextNode(inText);//建立文本節點
var
newGraf=document.createElement('p');//建立元素節點
newGraf.appendChild(newText);//將文本文本添加到元素內
var
docBody=document.getElementsByTagName('body')[0];
docBody.appendChild(newGraf);
document.getElementById('textArea').value="";
return
false;
}
/*function delNode(){
return false;
}
2》
window.onload=initAll;
function
initAll(){
document.getElementById('form1').onsubmit=function(){return
addNode();}
//document.getElementById('delNode').onclick=delNode;
}
function
addNode(){
var inText=document.getElementById('textArea').value;
var
newText=document.createTextNode(inText);//建立文本節點
var
newGraf=document.createElement('p');//建立元素節點
newGraf.appendChild(newText);//將文本文本添加到元素內
var
docBody=document.getElementsByTagName('body')[0];
docBody.appendChild(newGraf);
document.getElementById('textArea').value="";
return
false;
}
/*function delNode(){
return false;
}*/
心細的人可能一眼就能夠看的出來,可是我不屬於這一類優秀的選手,其實區別就是在於函數function delNode()處,第一個是/*function
delNode(){ return false;},第二個是/*function delNode(){return
false;}*/,哈哈區別就是在於/**/注釋符是不是寫完整了,如果沒有沒有寫完整就會出現JavaScript Error:unterminated
comment,什麼意思呢,就是有不完整的內容,程式就是無效的,這不是什麼複雜的事情,卻浪費了我很長時間,寫出來與大家共勉一下!!!