What properties can I use with event.target?

后端 未结 7 2052
醉酒成梦
醉酒成梦 2020-12-07 07:39

I need to identify elements from which events are fired.

Using event.target gets the respective element.

What properties can I use from there?<

7条回答
  •  孤城傲影
    2020-12-07 08:39

    event.target returns the DOM element, so you can retrieve any property/ attribute that has a value; so, to answer your question more specifically, you will always be able to retrieve nodeName, and you can retrieve href and id, provided the element has a href and id defined; otherwise undefined will be returned.

    However, inside an event handler, you can use this, which is set to the DOM element as well; much easier.

    $('foo').bind('click', function () {
        // inside here, `this` will refer to the foo that was clicked
    });
    

提交回复
热议问题