Getting the ID of the element that fired an event

2018-12-31 01:38发布

Is there any way to get the ID of the element that fires an event?

I'm thinking something like:

<html>
  <head>
    <script type="text/javascript" src="starterkit/jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function () {
        $("a").click(function () {
          var test = caller.id;
          alert(test.val());
        });
      });
    </script>
  </head>
  <body>
    <form class="item" id="aaa">
      <input class="title"></input>
    </form>
    <form class="item" id="bbb">
      <input class="title"></input>
    </form>
  </body>

</html>

Except of course that the var test should contain the id "aaa", if the event is fired from the first form, and "bbb", if the event is fired from the second form.

19条回答
若你有天会懂
2楼-- · 2018-12-31 02:34

Just use the this reference

$(this).attr("id")

or

$(this).prop("id")
查看更多
登录 后发表回答