第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問為什么把代碼精簡后運行達不到效果。

請問為什么把代碼精簡后運行達不到效果。

weibo_和諧_漢子_0 2017-04-27 10:05:06
<!DOCTYPE?html> <html> <head> ????<title>?new?document?</title> ????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"/> ????<script?type="text/javascript"> ????????function?bgcChange(obj){ ????????????obj.onmouseover=function(){ ????????????????obj.style.backgroundColor="#f2f2f2"; ????????????} ????????????obj.onmouseout=function(){ ????????????????obj.style.backgroundColor="#fff"; ????????????} ????????} ????????window.onload?=?function(){//進入頁面時,對已經(jīng)有的td注冊鼠標事件。 ????????????//?鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。 ????????????var?ttd=document.getElementsByTagName("td"); ????????????for(var?i=0;i<ttd.length;i++){//執(zhí)行完所有循環(huán)后,給每一個td注冊onmuseover和onmouseout事件; ????????????????bgcChange(ttd[i]); ????????????} ????????} ????</script> ????<style?type="text/css"> ????????td{ ????????????width:?30%; ????????????height:?20px; ????????} ????</style> </head> <body> <table?border="1"?width="50%"?id="table"> ????<tr> ????????<th>學號</th> ????????<th>姓名</th> ????????<th>操作</th> ????</tr> ????<tr> ????????<td>xh001</td> ????????<td>王小明</td> ????????<td><a?href="javascript:;"?onclick="del(this)">刪除</a></td>???<!--在刪除按鈕上添加點擊事件??--> ????</tr> ????<tr> ????????<td>xh002</td> ????????<td>劉小芳</td> ????????<td><a?href="javascript:;"?onclick="del(this)">刪除</a></td>???<!--在刪除按鈕上添加點擊事件??--> ????</tr> </table> </body> </html>?function bgcChange(obj){ ? ? ? ? ? ?obj.onmouseover=function(){ ? ? ? ? ? ? ? ?obj.style.backgroundColor="#f2f2f2"; ? ? ? ? ? ?} ? ? ? ? ? ?obj.onmouseout=function(){ ? ? ? ? ? ? ? ?obj.style.backgroundColor="#fff"; ? ? ? ? ? ?} ? ? ? ?} ? ? ? ?window.onload = function(){ ? ? ? ? ? ?var ttd=document.getElementsByTagName("td"); ? ? ? ? ? ?for(var i=0;i<ttd.length;i++){ ? ? ? ? ? ? ? ?bgcChange(ttd[i]); ? ? ? ? ? ?} ? ? ? ?}其中上面的代碼為什么不能簡寫成下面的:試過了,下面的代碼運行達不到想要的效果。?window.onload = function(){? ? ? ? ? ?var ttd=document.getElementsByTagName("td");? ? ? ? ? ?for(var i=0;i<ttd.length;i++){? ? ? ? ? ? ?? ?ttd[i].onmouseover=function(){? ? ? ? ? ? ? ??????????ttd[i].style.backgroundColor="#f2f2f2";? ? ? ? ? ?????}? ? ? ? ? ?????ttd[i].onmouseout=function(){? ? ? ? ? ????????? ? ??ttd[i].style.backgroundColor="#fff";? ? ? ? ? ?????}????????????????}? }? ? ? ? ? ?? ? ? ?
查看完整描述

1 回答

?
tom的貓

TA貢獻65條經(jīng)驗 獲得超35個贊

window.onload?=?function(){
???????????var?ttd=document.getElementsByTagName("td");
???????????for(var?i=0;i<ttd.length;i++){
????????????????ttd[i].onmouseover=function(){
????????????????????????this.style.backgroundColor="#f2f2f2";
???????????????}
???????????????ttd[i].onmouseout=function(){
????????????????????????this.style.backgroundColor="#fff";
???????????????}????
????????????}
		}

因為js執(zhí)行順序是按照代碼塊來進行預處理和執(zhí)行的,也就是說預處理的只是執(zhí)行到的代碼塊的聲明函數(shù)和變量,而對于還未加載的代碼塊,是沒法進行預處理的,上面的代碼中會先執(zhí)行for循環(huán)的代碼塊,然后再執(zhí)行鼠標事件的代碼塊。在執(zhí)行鼠標事件的代碼塊時for循環(huán)已經(jīng)走完了,此時 i== ttd.length,鼠標點擊事件里的 ttd[i] 是拿不到的

查看完整回答
反對 回復 2017-04-27
  • 1 回答
  • 0 關(guān)注
  • 1108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號