鼠標移動變色的問題
這是我自己的代碼語言實現(xiàn)鼠標變色功能,期間也參考了正解和他人的代碼都沒有看到變色效果,是代碼問題嗎?還是瀏覽器問題?
function?onload()?{
????var?tbody?=?document.getElementById('table').lastChild;
????var?trlist?=?tbody.getElementsByTagName('tr');
????for?(var?i?=?1;?i?<?trlist.length;?i++)?{
????????trlist[i].onmouseover?=?function?()?{
????????????this.style.backgroundColor?=?"#f2f2f2";
????????}
????????trlist[i].onmouseout?=?function?()?{
????????????this.style.backgroundColor?=?"#fff";
????????}
????}
}
2016-05-09
你這只是定義了一個名為onload的函數(shù)。window.onload = function(){}是頁面加載完后將執(zhí)行這里的匿名函數(shù)。
2016-05-05
i=0,應(yīng)該是從0開始的,如果前面有window.onload的話,把function onload去掉試試,試了一下是可以的。
2016-05-05
window.onload = function(){ ? ? ? ? ? ? ?
? ? ?// 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。
? ? ? ? ?var tdarr = document.getElementsByTagName("tr");
? ? ? ? ? for (var i=0; i< tdarr.length;i++){
? ? ? ? ? ? ? ? tdarr[i].onmouseover = function(){
? ? ? ? ? ? ? ? ? ? this.style.backgroundColor ="#f2f2f2";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? tdarr[i].onmouseout = function(){
? ? ? ? ? ? ? ? ? ? this.style.backgroundColor ="#fff";
? ? ? ? ? ? ? ? }
? ? ? ?} ? ? ? ?
}
這事我的最后一個練習(xí)題的代碼,你看下吧