get data attribute value onclick link / button

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

        
6条回答
  •  无人共我
    2021-02-20 12:25

    Your code is fine you have same id for more then one element which is not valid. The event will be binded to first element with given id in the DOM. Use common class instead of id to bind the event. You can use class selector to bind the event.

    Live Demo

    $('.btn').click(function() {
          var d = $(this).data('datac');      
          alert(d);   
    });
    

提交回复
热议问题