search

Home  >  Q&A  >  body text

javascript - How to find the position number of an element among its sibling elements in native js?

How to get it? These p's are all sibling elements. How to get the sequence number of one of these p's?

<p</p>//序号0
<p</p>//序号1
<p</p>//序号2
<p</p>//序号3
<p</p>//序号4
世界只因有你世界只因有你2778 days ago842

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:11:19

    function index(el) {
      return Array.prototype.indexOf.call(el.parentElement.children, el)
    }

    reply
    0
  • 某草草

    某草草2017-05-19 10:11:19

    Read in a loop through the prevSiblings of the element, and read a count+1 until no more is read. The final count is the index

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:11:19

    Determine how many nodes there are in front of a certain p, and add one for each additional serial number, until there is no node in front, and then the serial number value of the current node is obtained

    reply
    0
  • Cancelreply