请问为什么非得用 this 而不可以用aLi[i]呢?

window.onload=function () {
   var aLi=document.getElementsByTagName('a');
   for (var i=0; i<aLi.length; i++) {
       aLi[i].onclick=function () {
           this.parentNode.style.display='none';
       }
   }
}

lili13
浏览 1032回答 3
3回答

qq_南城旧梦_2

同步执行, 循环不会等待你的点击所触发的函数的执行,当你点击的时候,全局变量i已经经过循环变为了length

慕的地8582982

IWFPTWGMSQNCUXRUNAQZMMHWXHVVCCLRYKEDOECLLTZQROYBBANBLIUKKDLPTGZSTPLULRXIHYTSKOPGPRKAADKTUTEULOZGMIZOBOOLFBBQXOEHEKUKQNMDWEXPPCXNOEKOZPYTIICJZPJQTSIYPIZIFZSOEORBEBHKKKDUNXAXXQNROXGWGJJQFHXLXNYYUURGRHWPWQEPGXWGXKXNNXUTAOEYEHTDQEUQOCWBBUUDNDTBYBELSEIPOOXLEANTQWFTMZCMTWCOMWPUHEXQZCSIYVMLOFPLKUWMDQJDDHTWQJUAEDEYICOYLNDKXEFLBUUFYBIQRLJWMPILWGSPPBDXNELUBXHXJKLPZICCYFZPUNNWZMXGXQVIOUUOOUTQTXDWGQAEXJDTKADTDKNILLVDYHHRRCVLBLFMBLFORHNNBSUUYLZJFYFMHHMUUDXUJANWTMGZTNFYFJVOFFPIZOKRXEBNDNXHOXRBNXLPMPRUKGDTQGDYIETPTCGBKNQZPMVFVWLBLULHKTQZPSPMJZQMHXPEHJHXAWZOEHNEOXUXADZVYADAYIVFBXAQELUXGYTJZIRILAIRIMBKUEFIFUENXZPYUYODAQUXNJMXRUXFQTCSULBKUXHEUPLHXIXGPFCYHXNDLWFIYIRALYVSWSCGDMPFBKAWMIFOYVWSPZXAJZVFBFMWZNQYBSIRNJNQWTNWTIRUDTKHJFJHJAQZCMSUYGROXNJSJCFIEPMPFVYBLMCMORUXNDGEHQZWMCMISIYJFBSJXNVGWISVFCYPLULUHKTKGVLIDALWGJGCEUFOXGCGQZPFIFHKIVLARGWFVRYVPLAQGVYUQAQAJSCSVFIYNXQKNOSSCMLIPUVIOYJCQDZT
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript