Finding DOM node index

前端 未结 7 1949
死守一世寂寞
死守一世寂寞 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:55

    The shortest possible way, without any frameworks, in all versions of Safari, FireFox, Chrome and IE >= 9:

    var i = Array.prototype.indexOf.call(e.childNodes, someChildEl);

提交回复
热议问题