JavaScript,Jquery查找已单击元素的索引

JavaScript,Jquery查找已单击元素的索引,javascript,jquery,Javascript,Jquery,我正在构建幻灯片,我需要在下面的问题上进行堆栈-单击标记时如何在数组中找到位置(索引)?这部分代码将获得所有标记 this.thumbs = this.nav.find('a'); 从那里怎么走? 此外,还有另一个问题——我需要在标记内切换div的类(当单击标记时,div标记需要使类promo_tumb_最新,而具有该标记的一个需要松开它) HTML代码: <div class="promo_tumbs col_12"> <div data-dir="prev" c

我正在构建幻灯片,我需要在下面的问题上进行堆栈-单击标记时如何在数组中找到位置(索引)?这部分代码将获得所有标记

 this.thumbs = this.nav.find('a');
从那里怎么走? 此外,还有另一个问题——我需要在标记内切换div的类(当单击标记时,div标记需要使类promo_tumb_最新,而具有该标记的一个需要松开它)

HTML代码:

<div class="promo_tumbs col_12">
    <div data-dir="prev" class="prev"></div>
    <div data-dir="next" class="next"></div>
    <div class="promo_tumbs_centar">
        <a href="#first"><div class="promo_tumb promo_tumb_current"></div></a>
        <a href="#second"><div class="promo_tumb"></div></a>
        <a href="#thrid"><div class="promo_tumb"></div></a>
        <a href="#fourh"><div class="promo_tumb"></div></a>
        <a href="#fifth"><div class="promo_tumb"></div></a>
        <a href="#fifth"><div class="promo_tumb"></div></a>
        <a href="#fifth"><div class="promo_tumb"></div></a>
    </div>
</div>

JS代码:

<script>
                function Slider(container, nav){
                    this.container = container;
                    this.nav = nav;

                    this.li = this.container.find('li');
                    this.li_width = this.li.first().width();
                    this.li_len = this.li.length;

                    this.thumbs = this.nav.find('a');

                    this.current = 0;
                }

                Slider.prototype.transition = function (coords){
                    this.container.stop().animate({
                        'margin-left' : coords || -(this.current * this.li_width)
                    })
                }

                Slider.prototype.set_current = function(dir){
                    var pos = this.current;
                    if (dir === 'next') {pos++}
                    else if (dir === 'prev') {pos--}                
                    this.current = (pos < 0) ? this.li_len - 1 : pos % this.li_len;

                    return pos;
                }

                var slider = new Slider($('div.promo_inner ul'), $('div.promo_tumbs'));
                    slider.nav.find('div').on('click', function(){                               
                    if ($(this).attr("data-dir") === undefined ) {
                        var index = slider.thumbs.index();

                        console.log(index)
                    } else {
                         slider.set_current($(this).data('dir'));                    
                    }               
                    slider.transition();


                })

功能滑块(容器、导航){
this.container=容器;
this.nav=nav;
this.li=this.container.find('li');
this.li_width=this.li.first().width();
this.li_len=this.li.length;
this.thumbs=this.nav.find('a');
该电流=0;
}
Slider.prototype.transition=函数(coords){
此.container.stop().animate({
“左边距”:坐标| |-(this.current*this.li_width)
})
}
Slider.prototype.set_current=函数(dir){
var pos=该电流;
如果(dir=='next'){pos++}
如果(dir==='prev'){pos--}
this.current=(pos<0)?this.li_len-1:pos%this.li_len;
返回pos;
}
变量滑块=新滑块($('div.promo_inner ul'),$('div.promo_tumbs');
slider.nav.find('div')。on('click',function(){
if($(this).attr(“数据目录”)==未定义){
var index=slider.thumbs.index();
console.log(索引)
}否则{
slider.set_current($(this.data('dir'));
}               
slider.transition();
})

我想你需要的是

例如,在事件处理程序中(这是单击的标记):


你的代码是反向的。您正在
thumbs
中搜索
this
的索引,因此它应该是:
var index=thumbs.index(this)。当我尝试此操作时,结果为-1。我需要在thumbs
var index=$(this.index)(slider.thumbs)
之前添加滑块,否则会产生错误。@Sasha可能是因为Anthony Grist注意到的错误。示例代码已修复,Sorry我仍然得到-1作为o.o?这意味着单击的链接不在“thumbs”选项中。您必须使用调试器来查找原因。。。可能是某个愚蠢的地方。请检查这实际上是一个链接,并且thumbs实际上是您的链接集合。
var index = thumbs.index($(this))