javascript - How to find the position number of an element among its sibling elements in native js?
世界只因有你
世界只因有你 2017-05-19 10:09:19
0
3
783

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
世界只因有你
世界只因有你

reply all(3)
滿天的星座
function index(el) {
  return Array.prototype.indexOf.call(el.parentElement.children, el)
}
某草草

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

大家讲道理

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template