get value of an attribute of the clicked element

后端 未结 3 1782
梦谈多话
梦谈多话 2020-12-29 21:01
  • english
  • francais
  • italiano
相关标签:
3条回答
  • 2020-12-29 21:39

    This should do the job:

    $(document).ready(function() {
    
      $('#langs li').click(function() {
        alert($(this).attr('data-val'));
      });
    });
    

    Have a look at the Docs

    0 讨论(0)
  • 2020-12-29 21:41
    $('li').click(function() {
        alert($(this).attr('data-val'));
    });
    
    0 讨论(0)
  • 2020-12-29 21:49

    Original answer - 2011

    $('li').click(function () {
        alert($(this).data('val'));
    });
    

    See DEMO.

    Update - 2017

    Keep in mind that if you want to use the ES6 arrow function syntax, you cannot use this and you need to use e.currentTarget instead, where e is the event object passed as the first parameter to the event handler:

    $('li').click(e => alert($(e.currentTarget).data('val')));
    

    See DEMO.

    0 讨论(0)
提交回复
热议问题