類似jquery 的 .index();就一級,類似<ul id="myul"><li>click</li><li>click</li><li>click</li><li>click</li><li>click</li></ul> var nodeList = document.getElementsByTagName('li'); for(var i = 0;i<nodeList.length;i++){ //nodeList[i].index = i nodeList[i].addEventListener("click", function(e){ console.log(nodeList.indexOf(nodeList[i])) }, false); }我寫的貌似有了閉包的問題,更正我的代碼也好,我亂掉了。。。。請問要如何實現呢?
一道前端面試題,用純js返回點擊元素在父級元素內的位置
翻過高山走不出你
2018-08-02 16:02:06