get value of an attribute of the clicked element

后端 未结 3 1798
梦谈多话
梦谈多话 2020-12-29 21:01
  • english
  • francais
  • italiano
3条回答
  •  刺人心
    刺人心 (楼主)
    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.

提交回复
热议问题