How to get the index of the element in javascript?

后端 未结 5 749
北海茫月
北海茫月 2021-02-07 15:09

The NodeList don\'t have a indexOf(element) method? So, how can I get the element index?

5条回答
  •  旧巷少年郎
    2021-02-07 15:35

    You can use Array.prototype.indexOf.call() like this

    let nodes = document.getElementsByTagName('*');
    Array.prototype.indexOf.call(nodes, document.body);
    

提交回复
热议问题