對象的屬性設置問題
tr是創建的行的對象
1.按照下面代碼,
tr.lastChild.firstChild.onclick="deleteNode(this);";
tr.onmouseover="this.style.background='yellow'";
tr.onmouseout="this.style.background='#fff'";
console.log("打印tr.lastChild.firstChild.onclick內容:"+tr.lastChild.firstChild.onclick);
console.log("打印tr.onmouseover內容:"+tr.onmouseover);
console.log("打印tr.onmouseout內容:"+tr.onmouseout);
通過console.log輸出的都為null,不能正常執行,為什么???
2.按照下面代碼
tr.lastChild.firstChild.onclick=function(){
deleteNode(this);
}
tr.onmouseover=function(){
this.style.background="yellow";?
}
tr.onmouseout=function(){
this.style.background="#fff";?
}
通過console.log輸出的都正常,能夠正常執行。
3.通過下列代碼
tr.lastChild.firstChild.setAttribute("onclick","deleteNode(this);");
tr.setAttribute("onmouseover","this.style.background='yellow'");
tr.setAttribute("onmouseout","this.style.background='#fff'");
通過console.log輸出的都正常,能夠正常執行。
2016-12-22
第一個里面目測是無法將字符串類型的對象隱式轉換為事件的類型。或者說程序并不知道你給OnClick事件賦值的結果是什么。
第二個,很明顯的通過調用函數來執行你需要執行的刪除操作,沒有問題。
第三個,通過設置節點的屬性,類似與你在創建節點的時候,就已經在里面寫入onclick="deleteNode(this)"。
是我的一些看法,希望對你有所幫助。