對(duì)象的屬性設(shè)置問(wèn)題
tr是創(chuàng)建的行的對(duì)象
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內(nèi)容:"+tr.lastChild.firstChild.onclick);
console.log("打印tr.onmouseover內(nèi)容:"+tr.onmouseover);
console.log("打印tr.onmouseout內(nèi)容:"+tr.onmouseout);
通過(guò)console.log輸出的都為null,不能正常執(zhí)行,為什么???
2.按照下面代碼
tr.lastChild.firstChild.onclick=function(){
deleteNode(this);
}
tr.onmouseover=function(){
this.style.background="yellow";?
}
tr.onmouseout=function(){
this.style.background="#fff";?
}
通過(guò)console.log輸出的都正常,能夠正常執(zhí)行。
3.通過(guò)下列代碼
tr.lastChild.firstChild.setAttribute("onclick","deleteNode(this);");
tr.setAttribute("onmouseover","this.style.background='yellow'");
tr.setAttribute("onmouseout","this.style.background='#fff'");
通過(guò)console.log輸出的都正常,能夠正常執(zhí)行。
2016-12-22
第一個(gè)里面目測(cè)是無(wú)法將字符串類型的對(duì)象隱式轉(zhuǎn)換為事件的類型。或者說(shuō)程序并不知道你給OnClick事件賦值的結(jié)果是什么。
第二個(gè),很明顯的通過(guò)調(diào)用函數(shù)來(lái)執(zhí)行你需要執(zhí)行的刪除操作,沒有問(wèn)題。
第三個(gè),通過(guò)設(shè)置節(jié)點(diǎn)的屬性,類似與你在創(chuàng)建節(jié)點(diǎn)的時(shí)候,就已經(jīng)在里面寫入onclick="deleteNode(this)"。
是我的一些看法,希望對(duì)你有所幫助。