jQuery Button Click 'this'

后端 未结 3 741
走了就别回头了
走了就别回头了 2021-01-25 16:58

Say we have a simple event button handler:

$( \"#aButton\" ).click(function() {
   console.log( \"tile\" + this.val() + \"clicked\" );
});

I re

3条回答
  •  逝去的感伤
    2021-01-25 17:05

    Use $(this) to obtain the clicked element.

    $("#aButton").click(function() {
        console.log("tile " + $(this).val() + " clicked");
    });
    

    In addition, you can note that $(this)[0] === this

    this should can be used when using built in JavaScript methods, such as .addEventListener(). Using this in this context will return the Node that triggered the event.

提交回复
热议问题