jquery index within a parent class

wil*_*.am 5 css indexing jquery element count

I need a way to find the index number of one child element.

here is the CSS of it

<div class="parent">
    <div class="son"></div>
    <div class="son"></div>
    <div class="son"></div>
    <div class="son"></div>
    <div class="son"></div>
</div>

<div class="parent">
    <div class="son"></div>
    <div class="son"></div>
    <div class="son"></div>
    <div class="son"></div>
    <div class="son"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

My jquery code is like this

var number = $(".son").index(this);
Run Code Online (Sandbox Code Playgroud)

When I use this code, it will count the son as a whole. Eg, when I click on the second child element in the second parent class, it will give me a var number 7. I want the son class always start counting from zero.

Tom*_*mek 11

试试这个:

$(this).parent().find('.son').index(this)
Run Code Online (Sandbox Code Playgroud)

正如其他成员所说:

$(this).index()
Run Code Online (Sandbox Code Playgroud)

是作业,因为没有参数的index()返回元素相对于其兄弟的位置.

文档:

如果没有将参数传递给.index()方法,则返回值是一个整数,指示jQuery对象中第一个元素相对于其兄弟元素的位置.