get parentNode of clicked element in plain JS

天大地大妈咪最大 提交于 2019-12-04 09:14:51
function new_class(event) {
    wTile = event.target.parentNode;
    wTile.className = wTile.className + " added-class";
}

"I just don't know how to integrate this in the script"

Use .call() to invoke the handler to set its this value to the element that has the handler...

<div id="item_click" onmousedown="new_class.call(this,event)" ...>

function new_class(event) {
    var wTile = this.parentNode;
    wTile.className = wTile.className + " added-class";
}

You can just use event.target.parentNode (Don't need to even pass the event in the function):

function new_class() {
    var parent = event.target.parentNode;
    // Do whatever...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!