Is it possible to get element's numerical index in its parent node without looping?

前端 未结 7 1396
攒了一身酷
攒了一身酷 2020-11-30 07:21

Normally I\'m doing it this way:

for(i=0;i
7条回答
  •  渐次进展
    2020-11-30 07:40

    You could count siblings... The childNodes list includes text and element nodes-

    function whichChild(elem){
        var  i= 0;
        while((elem=elem.previousSibling)!=null) ++i;
        return i;
    }
    

提交回复
热议问题