父类中的jquery索引 [英] jquery index within a parent class

查看:106
本文介绍了父类中的jquery索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来找到一个子元素的索引号。

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

这里是它的CSS

<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>

我的jquery代码如下

My jquery code is like this

var number = $(".son").index(this);

当我使用这个代码时,它会将儿子作为一个整体。例如,当我点击第二个父类中的第二个子元素时,它将给我一个变数7.我想让子类总是从零开始计数。

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.

推荐答案

尝试这样:

$(this).parent().find('.son').index(this)

其他成员提到:

$(this).index()


$ b b

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

Does the job, since index() with no arguments returns position of element relative to its siblings.

文档:


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

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

这篇关于父类中的jquery索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆