Get value of List Item with jQuery

后端 未结 5 973
后悔当初
后悔当初 2021-02-08 07:19

How to get value and index of list item onClick event with jQuery?
for example:

  • Item 1
  • Item 2&
5条回答
  •  -上瘾入骨i
    2021-02-08 07:44

    Combine the use of .index() and .text() (or .html(), if you wish):

    $('#uItem li').click(function() {
        var index = $(this).index();
        var text = $(this).text();
        alert('Index is: ' + index + ' and text is ' + text);
    });
    

提交回复
热议问题