查找特定类的元素的Dom节点索引 [英] Finding Dom Node Index for elements of a certain class

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

问题描述

我有一系列带有不同类别的标签.单击跨度时,我想返回跨度类的索引.因此跨度本身的索引不是.

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.

这是一个示例html:

Here is a sample html:

<span class='spantype1'>text1</span>
<span class='spantype2'>text2</span>
<span class='spantype1'>text3</span>

因此,如果我单击text3,我想返回1而不是2.

So if I click on text3 I want to return 1 not 2.

此处的答案无效:

$( "span" ).click(function() {
    var index = $(this).parent().children().index(this);
alert(index);
});

我的意思是,如果我单击text3,我返回1,如果我单击text1,我返回0.我很抱歉.当然,如果我单击text2,我将返回0.

What I meant was if I click on text3 I return 1, and if I click on text1 I return 0. My apologies. And of course if I click on text2 I return 0.

推荐答案

尝试

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

这篇关于查找特定类的元素的Dom节点索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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