Fastest way to find the index of a child node in parent

前端 未结 9 1559
醉梦人生
醉梦人生 2020-12-13 07:06

I want to find the index of the child div that has the id \'whereami\'.

9条回答
  •  死守一世寂寞
    2020-12-13 07:14

    By co-opting Array indexOf you could use :

      var wmi = document.getElementById ('whereami');
      index = [].indexOf.call (wmi.parentNode.children, wmi);
    

    [Tested on Chrome browser only]

提交回复
热议问题