get data attribute value onclick link / button

后端 未结 6 1960
攒了一身酷
攒了一身酷 2021-02-20 11:50
Daily
            

        
6条回答
  •  说谎
    说谎 (楼主)
    2021-02-20 12:23

    This should do the trick:

    var d = $(this).attr("data-datac");
    

    Also, you need to change your HTML to give each element a seperate id. But it can be a tedious task to bind click events for a number of different ids. So i'd bind it to a seperate class. For example:

    HTML

    Daily
    1 Week
    Month
    3 Month
    

    Javascript

    $(".dateChanger").click(function () {
        var d = $(this).attr("data-datac");
        alert(d);
    });
    

提交回复
热议问题