我可以将哪些属性用于 event.target? [英] What properties can I use with event.target?
问题描述
我需要确定触发事件的元素.
I need to identify elements from which events are fired.
使用 event.target
获取相应的元素.
Using event.target
gets the respective element.
我可以从那里使用哪些属性?
What properties can I use from there?
- 引用
- id
- 节点名称
我找不到关于它的大量信息,即使在 jQuery 页面上,所以在这里希望有人能完成上面的列表.
I cannot find a whole lot of info on it, even on the jQuery pages, so here is to hoping someone can complete the above list.
这些可能会有帮助:selfHTML 节点属性和selfHTML HTML 属性
These may be helpful: selfHTML node properties and selfHTML HTML properties
推荐答案
event.target
返回 DOM 元素,因此您可以检索任何具有值的属性/属性;因此,为了更具体地回答您的问题,您将始终能够检索 nodeName
,并且您可以检索 href
和 id
,前提是该元素有定义了一个href
和id
;否则将返回 undefined
.
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.
但是,在事件处理程序中,您可以使用 this
,它也设置为 DOM 元素;容易多了.
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
});
这篇关于我可以将哪些属性用于 event.target?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!