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>
漂亮男人2017-07-05 10:55:27
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
伊谢尔伦2017-07-05 10:55:27
[...document.querySelector('.b').parentNode.parentNode.children].indexOf(document.querySelector('.b').parentNode)
曾经蜡笔没有小新2017-07-05 10:55:27
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