2011-10-11 93 views

回答

24

event.target返回的DOM元素,这样你就可以检索到任何属性/属性具有值;因此,为了更具体地回答您的问题,您将始终能够检索nodeName,并且您可以检索hrefid,前提是要素具有 a hrefid定义;否则将返回undefined

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

​​
+0

啊乘虚而入。谢谢!我正在准备显示可用属性的链接。 – frequent

+2

将“this”引用“foo”还是“event.target”?如果我在文档上收听scrollStart,并在H1元素上触发scrollStart,我该如何获取H1? – frequent

+0

这是做到了。谢谢。 – frequent

2

event.target返回由功能有针对性的节点。这意味着你可以做任何事情,你会做任何其他节点就像你会得到一个document.getElementById

72

如果你要检查event.target与萤火虫或铬的开发人员工具,你会看到一个跨度元素(例如以下属性)它将具有任何元素具有的任何属性。它取决于目标元素是什么:

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 
+6

据我所知,chrome在使用console.log时不再像这样打印DOM元素。您必须改用console.dir。 – Astridax

+0

是的,这不是太糟糕! –

5

event.target返回该函数的目标节点。这意味着你可以做你想做与像任何其它节点做任何事情,你会得到从document.getElementById

我用jQuery

var _target = e.target; 
console.log(_target .attr('href')); 

试图返回一个错误:

。 attr不起作用

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

+2

这是因为'e.target'不是一个jQuery对象,'.attr()'是jQuery的方法。如果你想'__target.getAttribute('href');'它会工作得很好。 – Kano

4
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 
} 

enter image description here

使用过滤器化子性质


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()` 
+0

谢谢!!!,对我来说,最好的答案。 –

相关问题