javascript - How to get the number of the parent element of a future element among all the same elements on the page?
淡淡烟草味
淡淡烟草味 2017-07-05 10:53:27
0
3
798

The entire code is dynamically added to the page through js. When b changes, how to get the position (position) of a among all a's on the page?

<p class="a">
    <p class="b"></p>
</p>
淡淡烟草味
淡淡烟草味

reply all(3)
漂亮男人

I don’t quite understand what it means

If the class name of a is unchanged, use jQuery directly

$(".a").index(this.parent())

where this is the changed b

伊谢尔伦
[...document.querySelector('.b').parentNode.parentNode.children].indexOf(document.querySelector('.b').parentNode)
曾经蜡笔没有小新

Since it is added dynamically, why not give it an identifier before adding it to the dom? For example:

<p class="a" index="1">
    <p class="b"></p>
</p>
<p class="a" index="2">
    <p class="b"></p>
</p>
...

In this way, when b changes, just get the parent’s index attribute directly

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