表格鼠标悬停变色。
代码:
var tr=document.getElementsByTagName('tr'); for (var i = 0; i < tr.length; i++) { tr[i].onmouseover=function(){ tr[i].style.backgroundColor="#f2f2f2"; } tr[i].onmouseleave=function(){ tr[i].style.backgroundColor="#fff"; } }
chrome说的是 Uncaught TypeError: Cannot read property 'style' of undefined at HTMLTableRowElement.tr.(anonymous function).onmouseover
但是把tr[i]
改为this
就可以:
var tr=document.getElementsByTagName('tr'); for (var i = 0; i < tr.length; i++) { tr[i].onmouseover=function(){ this.style.backgroundColor="#f2f2f2"; } tr[i].onmouseleave=function(){ this.style.backgroundColor="#fff"; } }
杨__羊羊
相关分类