Finding Dom Node Index for elements of a certain class

后端 未结 2 588
旧时难觅i
旧时难觅i 2021-01-23 01:29

Hi I have a series of tags with different classes. When a span is clicked I want to return the index of the class of spans. So not the index of the spans themselves.

He

相关标签:
2条回答
  • 2021-01-23 02:04

    Try,

    $( "span" ).click(function() {
        var index = $('.' + $(this).attr('class')).index($(this));
        alert(index + 1);
    });
    

    DEMO

    0 讨论(0)
  • 2021-01-23 02:20

    Try this:

    var index = $(this).parent().find($(this)).index();
    
    0 讨论(0)
提交回复
热议问题