Set counter per element in jQuery plugin

前端 未结 3 1341
难免孤独
难免孤独 2021-01-28 07:11

When you set a counter in a jQuery plugin it will be set per instance of the plugin. For instance

$.myPlugin(\".one\");
$.myPlugin(\".two\");

$.myPlugin = funct         


        
3条回答
  •  生来不讨喜
    2021-01-28 07:21

    Use custom data- attributes to set a counter

    $('button').each(function(){
        $(this).attr('data-counter', 0);
        $(this).click(function () {
            var counter = parseInt($(this).attr('data-counter'));
            counter++;
            console.log(counter);
            $(this).attr('data-counter', counter);
        });
    });
    

    DEMO

提交回复
热议问题