Inline event handling attributes are not passed a reference to a DOM event and that's why your argument is undefined
.
Don't use inline HTML event attributes to hook up events. That's a 25+ year old technique that just will not die.
Follow modern standards and use .addEventListener()
which will automatically be passed a reference to the DOM event that triggered the handler.
document.querySelectorAll(".drag").forEach(function(item){
item.addEventListener("dragstart", startDrag);
});
function startDrag(e) {
console.log("Drag started! Event was: ", e.type);
}
<ul>
<li class="drag" draggable="true">Drag me.</li>
<li class="drag" draggable="true">No, drag me!</li>
</ul>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…