如何使用jQuery获取带有certin类的元素的索引,位置号

时间:2021-10-19 21:20:21

I have this markup

我有这个标记

<div class="parent">
    <div class="one"></div>
    <div class="two"></div>
    <div class="one"></div>
    <div class="two"></div>
</div>

My question is: how to get "index" number of element with class two. I'm not asking of regular index number of element in parent div. I need to know that when i'm clicking at first element with class one that next two element have 0 index or it's first element in this list with class two, and so on.

我的问题是:如何获得第二类元素的“索引”数。我不是要询问父div中元素的常规索引号。我需要知道,当我点击第一个元素与第一个元素时,接下来的两个元素有0个索引,或者它是这个列表中第一个元素的第二个元素,依此类推。

I've tried with index() method and eq() and always i have the real index number of this element in parent div. I hope this is clear, thx for help.

我已经尝试使用index()方法和eq(),并且总是在父div中拥有此元素的实际索引号。我希望这很明确,请求帮助。

4 个解决方案

#1


2  

This should be the faster way to get the index you are looking for.

这应该是获得您正在寻找的索引的更快捷方式。

Instead of retrieving all matches, it just counts the number of elements of the same class among previous leafs in your DOM.

它不是检索所有匹配,而是计算DOM中先前叶子中同一类的元素数。

Also, it allows having multiple <div class="parent"> and still work

此外,它允许多个

并且仍然有效

$('.parent div').click(function() {
    // Retrieve clicked element class (one, two)
    var elClass = $(this).attr('class');

    // Retrieve all previous elements that have the same class
    // and count them
    var prevElmts = $(this).prevAll('.' + elClass),
        numPrevElements = prevElmts.length;

    console.log(numPrevElements);
})

#2


2  

You need to find the elements index in collection of element with sameclass:

您需要在具有相同类的元素集合中找到元素索引:

$('.parent > div').click(function(){
 var index;
 if($(this).is('.one'))
    index = $('.parent > .one').index(this);
 else 
    index = $('.parent > .two').index(this);
});

#3


0  

This may work for you.

这可能对你有用。

var p = $('.parent'),
current = p.filter('.two'),
index = p.index(current);

#4


0  

Using jquery you can find index of element which have same class name or tag name

使用jquery,您可以找到具有相同类名或标记名的元素索引

$(".class_name").on("event_name", function() {
        var index=($(this).index());
        console.log('index : ',index);
});

#1


2  

This should be the faster way to get the index you are looking for.

这应该是获得您正在寻找的索引的更快捷方式。

Instead of retrieving all matches, it just counts the number of elements of the same class among previous leafs in your DOM.

它不是检索所有匹配,而是计算DOM中先前叶子中同一类的元素数。

Also, it allows having multiple <div class="parent"> and still work

此外,它允许多个

并且仍然有效

$('.parent div').click(function() {
    // Retrieve clicked element class (one, two)
    var elClass = $(this).attr('class');

    // Retrieve all previous elements that have the same class
    // and count them
    var prevElmts = $(this).prevAll('.' + elClass),
        numPrevElements = prevElmts.length;

    console.log(numPrevElements);
})

#2


2  

You need to find the elements index in collection of element with sameclass:

您需要在具有相同类的元素集合中找到元素索引:

$('.parent > div').click(function(){
 var index;
 if($(this).is('.one'))
    index = $('.parent > .one').index(this);
 else 
    index = $('.parent > .two').index(this);
});

#3


0  

This may work for you.

这可能对你有用。

var p = $('.parent'),
current = p.filter('.two'),
index = p.index(current);

#4


0  

Using jquery you can find index of element which have same class name or tag name

使用jquery,您可以找到具有相同类名或标记名的元素索引

$(".class_name").on("event_name", function() {
        var index=($(this).index());
        console.log('index : ',index);
});