首页 > 解决方案 > Making div element dynamic with another function

问题描述

I have a block of code that I want to try and refactor.

    $(function () {
        $('.dots:nth-of-type(1)').click(function () {
            currentSlide(1);
        });
        $('.dots:nth-of-type(2)').click(function () {
            currentSlide(2);
        });
    });

How can I make the nth-of-type(1) dynamic along with the currentSlide(1) so that for each nth-of-type(), I just need to click on it in order currentSlide to be the same?

标签: jquery

解决方案


使用 jQuery 的index 方法

$(function() {
    $('.dots').click(function() {
        currentSlide($(this).index());
    });
});

推荐阅读