jQuery get the id/value of
  • element after click function
  • 后端 未结 4 1031
    忘了有多久
    忘了有多久 2020-11-28 04:59

    How can I alert the id of the

  • item clicked?

    • First
    • Second
  • 4条回答
    •  一个人的身影
      2020-11-28 05:32

      If you change your html code a bit - remove the ids

      • First
      • Second
      • Third
      • Fourth
      • Fifth

      Then the jquery code you want is...

      $("#myid li").click(function() {
          alert($(this).prevAll().length+1);
      });​
      

      You don't need to place any ids, just keep on adding li items.

      Take a look at demo

      Useful links

      • jQuery prevAll

    提交回复
    热议问题