ฉันสามารถใช้คุณสมบัติอะไรกับ event.target ได้บ้าง


93

ฉันจำเป็นต้องระบุองค์ประกอบที่เกิดขึ้นจากเหตุการณ์

การใช้event.targetรับองค์ประกอบที่เกี่ยวข้อง

ฉันสามารถใช้คุณสมบัติอะไรได้บ้างจากที่นั่น?

  • href
  • id
  • nodeName

ฉันไม่สามารถหาข้อมูลทั้งหมดได้แม้แต่ในหน้าjQueryดังนั้นนี่คือการหวังว่าใครบางคนจะสามารถทำรายการด้านบนได้

แก้ไข:

สิ่งเหล่านี้อาจเป็นประโยชน์: คุณสมบัติโหนดselfHTMLและคุณสมบัติselfHTML HTML

คำตอบ:


42

event.targetส่งคืนองค์ประกอบ DOM เพื่อให้คุณสามารถดึงคุณสมบัติ / แอตทริบิวต์ที่มีค่าได้ ดังนั้นเพื่อตอบคำถามของคุณให้เจาะจงมากขึ้นคุณจะสามารถดึงข้อมูลได้nodeNameตลอดเวลาและคุณสามารถดึงข้อมูลhrefและidหากองค์ประกอบนั้นมีhrefและidกำหนดไว้ มิฉะนั้นundefinedจะถูกส่งกลับ

อย่างไรก็ตามภายในตัวจัดการเหตุการณ์คุณสามารถใช้thisซึ่งตั้งค่าเป็นองค์ประกอบ DOM ได้เช่นกัน ง่ายกว่ามาก.

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

อา. ขอบคุณ! ฉันใส่ลิงค์ซึ่งแสดงคุณสมบัติที่มีอยู่
บ่อย

2
"this" จะหมายถึง "foo" หรือ "event.target"? หากฉันกำลังฟัง scrollStart บนเอกสารและ scrollStart ถูกทริกเกอร์บนองค์ประกอบ H1 ฉันจะคว้า H1 ได้อย่างไร
บ่อย

@frequent - คำตอบของ Richards ด้านล่างแสดงให้เห็นว่าคุณสามารถหา H1 ได้โดยดูที่ event.target.tagName
Frazer Kirkman

144

หากคุณต้องตรวจสอบevent.targetด้วยเครื่องมือสำหรับนักพัฒนาของ firebug หรือ chrome คุณจะเห็นองค์ประกอบ 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

22
เท่าที่ฉันทราบว่า chrome ไม่พิมพ์องค์ประกอบ DOM ออกมาเช่นนี้อีกต่อไปเมื่อใช้ console.log คุณต้องใช้ console.dir แทน
Astridax

ใช่มันไม่เลวร้ายเกินไป!

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

ป้อนคำอธิบายภาพที่นี่

ใช้ประโยชน์จากการใช้คุณสมบัติตัวกรอง


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

1
<attribute> ชื่อ ... ทำไมในโลกนี้ไม่มีใครอื่นนอกจากคุณที่จะบอกคำตอบง่ายๆนี้?
karthikeayan

7

event.targetส่งคืนโหนดที่ถูกกำหนดเป้าหมายโดยฟังก์ชัน ซึ่งหมายความว่าคุณสามารถทำอะไรก็ได้ที่คุณต้องการทำกับโหนดอื่น ๆ ที่คุณต้องการdocument.getElementById

ฉันลองใช้ jQuery

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

ส่งคืนข้อผิดพลาด:

.attr ไม่ทำงาน

แต่_target.attributes.href.valueก็ใช้ได้ผล


10
นั่นเป็นเพราะe.targetไม่ใช่วัตถุ jQuery และ.attr()เป็นวิธีการของ jQuery ถ้าคุณลองใช้__target.getAttribute('href');มันก็ใช้ได้ดี
kano

2

event.targetส่งคืนโหนดที่ถูกกำหนดเป้าหมายโดยฟังก์ชัน ซึ่งหมายความว่าคุณสามารถทำอะไรก็ได้ที่คุณทำกับโหนดอื่น ๆ เช่นเดียวกับที่คุณได้รับdocument.getElementById


1

วิธีง่ายๆในการดูคุณสมบัติทั้งหมดบนโหนด DOM เฉพาะใน Chrome (ฉันใช้เวอร์ชัน 69) คือคลิกขวาที่องค์ประกอบเลือกตรวจสอบจากนั้นแทนที่จะดูแท็บ "สไตล์" ให้คลิกที่ "คุณสมบัติ" .

ภายในแท็บคุณสมบัติคุณจะเห็นคุณสมบัติทั้งหมดสำหรับองค์ประกอบเฉพาะของคุณ


ควรค่าแก่การกล่าวถึงว่าตอนนี้พาเนลนี้เลิกใช้งานแล้วเพื่อสนับสนุน console.dir บน Chrome 85
ivanji

0
//Do it like---
function dragStart(this_,event) {
    var row=$(this_).attr('whatever');
    event.dataTransfer.setData("Text", row);
}

3
คำถามนี้ได้รับคำตอบแล้วและมีการโหวตให้คำตอบมากมายสำหรับคำตอบเหล่านี้ คำตอบของคุณดีกว่าในทางไหน คุณช่วยอธิบายวิธีแก้ปัญหาของคุณได้ไหม
Quality Catalyst
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.