類似jquery 的 .index();就一級(jí),類似<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); }我寫的貌似有了閉包的問(wèn)題,更正我的代碼也好,我亂掉了。。。。請(qǐng)問(wèn)要如何實(shí)現(xiàn)呢?
一道前端面試題,用純js返回點(diǎn)擊元素在父級(jí)元素內(nèi)的位置
翻過(guò)高山走不出你
2018-08-02 16:02:06
