我可以在event.target中使用哪些属性?

fre*_*ent 74 jquery events properties

我需要识别触发事件的元素.

使用event.target获取相应的元素.

我可以在那里使用什么属性?

  • HREF
  • ID
  • 节点名称

即使在jQuery页面上,我也找不到很多关于它的信息,所以这里希望有人可以完成上面的列表.

编辑:

这些可能会有所帮助:selfHTML节点属性selfHTML HTML属性

Ric*_*ard 121

如果您要使用firebug或chrome的开发人员工具检查event.target,您会看到一个span元素(例如以下属性),它将具有任何元素具有的任何属性.它取决于目标元素:

event.target: HTMLSpanElement

attributes: NamedNodeMap
baseURI: "file:///C:/Test.html"
childElementCount: 0
childNodes: NodeList[1]
children: HTMLCollection[0]
classList: DOMTokenList
className: ""
clientHeight: 36
clientLeft: 1
clientTop: 1
clientWidth: 1443
contentEditable: "inherit"
dataset: DOMStringMap
dir: ""
draggable: false
firstChild: Text
firstElementChild: null
hidden: false
id: ""
innerHTML: "click"
innerText: "click"
isContentEditable: false
lang: ""
lastChild: Text
lastElementChild: null
localName: "span"
namespaceURI: "http://www.w3.org/1999/xhtml"
nextElementSibling: null
nextSibling: null
nodeName: "SPAN"
nodeType: 1
nodeValue: null
offsetHeight: 38
offsetLeft: 26
offsetParent: HTMLBodyElement
offsetTop: 62
offsetWidth: 1445
onabort: null
onbeforecopy: null
onbeforecut: null
onbeforepaste: null
onblur: null
onchange: null
onclick: null
oncontextmenu: null
oncopy: null
oncut: null
ondblclick: null
ondrag: null
ondragend: null
ondragenter: null
ondragleave: null
ondragover: null
ondragstart: null
ondrop: null
onerror: null
onfocus: null
oninput: null
oninvalid: null
onkeydown: null
onkeypress: null
onkeyup: null
onload: null
onmousedown: null
onmousemove: null
onmouseout: null
onmouseover: null
onmouseup: null
onmousewheel: null
onpaste: null
onreset: null
onscroll: null
onsearch: null
onselect: null
onselectstart: null
onsubmit: null
onwebkitfullscreenchange: null
outerHTML: "<span>click</span>"
outerText: "click"
ownerDocument: HTMLDocument
parentElement: HTMLElement
parentNode: HTMLElement
prefix: null
previousElementSibling: null
previousSibling: null
scrollHeight: 36
scrollLeft: 0
scrollTop: 0
scrollWidth: 1443
spellcheck: true
style: CSSStyleDeclaration
tabIndex: -1
tagName: "SPAN"
textContent: "click"
title: ""
webkitdropzone: ""
__proto__: HTMLSpanElement
Run Code Online (Sandbox Code Playgroud)

  • 据我所知,当使用console.log时,chrome不再像这样输出DOM元素.你必须使用console.dir. (17认同)

Mat*_*att 39

event.target返回DOM元素,因此您可以检索具有值的任何属性/属性; 因此,要回答你的问题更具体,你将永远无法找回nodeName,你可以检索hrefid,所提供的元素一个hrefid定义; 否则undefined将被退回.

但是,在事件处理程序中,您可以使用this,也设置为DOM元素; 更容易.

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

  • "this"会指"foo"还是"event.target"?如果我正在监听文档上的scrollStart并且在H1元素上触发scrollStart,我该如何抓取H1? (2认同)

bhv*_*bhv 15

window.onclick = e => {
    console.dir(e.target);  // use this in chrome
    console.log(e.target);  // use this in firefox - click on tag name to view 
}  
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

利用过滤器的优点


e.target.tagName
e.target.className
e.target.style.height  // its not the value applied from the css style sheet, to get that values use `getComputedStyle()`
Run Code Online (Sandbox Code Playgroud)

  • &lt;attribute&gt;姓名...为​​什么除了你之外没有人能说出这个简单的答案? (2认同)

小智 6

event.target返回该函数所针对的节点.这意味着您可以对任何其他节点执行任何操作,例如您可以使用的节点document.getElementById

我试过jQuery

var _target = e.target;
console.log(_target.attr('href'));
Run Code Online (Sandbox Code Playgroud)

返回错误:

.attr不起作用

但是_target.attributes.href.value有效.

  • 那是因为`e.target`不是jQuery对象而`.attr()`是jQuery的方法.如果你要尝试`__target.getAttribute('href');`它可以正常工作. (5认同)

Ale*_*lex 5

event.target返回函数所针对的节点。这意味着您可以对任何其他节点执行任何操作,就像您从其中获得的节点一样document.getElementById