var ulObj = document.getElementById("myUl"); ulObj.onclick = function (event) { var tg = event.target; var liArray = ulObj.getElementsByTagName("li");
for (var i = 0; i if (liArray[i] === tg) { alert🎜>if (liArray[i] === tg) { alert (i 1); }
} } 方式二,使用閉包:
複製程式碼
程式碼如下:
var liArray = document.getElementById("myUl").getElementsByTagName("li"); (var i = 0; i (function () { var n = i; liArray[i].onclick = function () { alert( n 1); }