【问题标题】:JavaScript, Jquery find index of clicked elementJavaScript,Jquery 查找被点击元素的索引
【发布时间】:2022-11-13 06:10:25
【问题描述】:

我构建幻灯片,我需要在以下问题中堆栈 - 单击标签时如何在数组中找到位置(索引)?这部分代码是获取所有标签

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

从那里怎么走? 而且,还有另一个问题 - 我需要在标签内切换 div 的类(当点击标签时,div 标签需要获取类 promo_tumb_current 并且具有该标签的类需要松开它)。

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();


                })

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    我认为你需要的是

    http://api.jquery.com/index/

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

    var index = thumbs.index($(this))
    

    【讨论】:

    • 你的代码是倒退的。您在thumbs 中搜索this 的索引,所以它应该是:var index = thumbs.index(this);
    • 当我尝试这个时,我得到 -1 的结果。我需要在拇指 var index = $(this).index(slider.thumbs) 之前添加滑块,否则会产生错误。
    • @Sasha 可能是因为 Anthony Grist 注意到的错误。示例代码已修复,抱歉
    • 由于oF,我仍然得到-1?
    • 这意味着单击的链接不在“拇指”选择中。你必须使用调试器来找出原因......可能在某个地方有些愚蠢。请检查这实际上是一个链接,并且拇指实际上是您的链接集合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2012-04-12
    相关资源
    最近更新 更多