Finding DOM node index

前端 未结 7 1948
死守一世寂寞
死守一世寂寞 2020-11-27 16:09

I want find the index of a given DOM node. It\'s like the inverse of doing

document.getElementById(\'id_of_element\').childNodes[K]

I want

7条回答
  •  無奈伤痛
    2020-11-27 16:57

    A modern native approach might include Array.from(e.children).indexOf(theChild)

    No IE support, but Edge works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

提交回复
热议问题