简体   繁体   English

只有event.target的目标父级?

[英]Only target parent with event.target?

HTML: HTML:

<div onclick="doSomething()" id="parent">
    <div id="child"></div>
</div>

CSS: CSS:

#parent {
    background-color: blue;
    width: 100%;
    height: 100px;
}

#child {
    background-color: green;
    width: 50%;
    height: inherit;
}

.myClass {
    background-color: red !important;
}

JS: JS:

function doSomething() {
    event.target.className = ('myClass');
}

As you can see in this JSFIDDLE , upon clicking the child, instead of applying the class to the parent which triggers the function, it applies it to the child. 正如您在此JSFIDDLE中所看到的,在单击子项时,不是将该类应用于触发该函数的父项,而是将其应用于子项。 I want to know how to avoid this and apply it to the parent no matter where I click inside of it. 我想知道如何避免这种情况并将其应用于父母,无论我在哪里点击它。 I am trying to avoid using the document.getElement(s)ByClass/Id method. 我试图避免使用document.getElement(s)ByClass/Id方法。
Any help? 有帮助吗?

You can refer to the element that handles the event with currentTarget . 您可以使用currentTarget引用处理事件的元素。

Identifies the current target for the event, as the event traverses the DOM. 当事件遍历DOM时,标识事件的当前目标。 It always refers to the element the event handler has been attached to as opposed to event.target which identifies the element on which the event occurred. 它始终引用事件处理程序附加到的元素,而不是event.target ,它标识事件发生的元素。


However, instead of relying on the browser to provide a global event object, I would pass it to the function: 但是,我不会依赖浏览器来提供全局event对象,而是将其传递给函数:

onclick="doSomething(event)"

You can also refer to the element the handler is bound to with this : 您也可以参照处理程序绑定到该元素this

onclick="doSomething(event, this)"

Of course please consider to not use inline event handlers . 当然请考虑不使用内联事件处理程序

If you use addEventListener to handle the event instead of the inline version, then the this of the callback will point to the element that the event was declared on (#parent) - fiddle : 如果您使用的addEventListener来处理该事件,而不是在线版本,那么this回调将指向该事件被宣布(#parent)的元素- 小提琴

<div id="parent">
    <div id="child"></div>
</div>

Put this code at the end of the body or in DOMContentLoaded event handler: 将此代码放在正文的末尾或DOMContentLoaded事件处理程序中:

function doSomething() {
    this.className = 'myClass';
}

document.getElementById('parent').addEventListener('click', doSomething);

Just reference the target in your javascript call: 只需在javascript调用中引用target

 function doSomething(target) { target.className = ('myClass'); } 
 #parent { background-color: blue; width: 100%; height: 100px; } #child { background-color: green; width: 50%; height: inherit; } .myClass { background-color: red !important; } 
 <div onclick="doSomething(this)" id="parent"> <div id="child"></div> </div> 

To get the immediate parent element of the clicked element you can use the 'path' array of the event. 要获取单击元素的直接父元素,可以使用事件的“path”数组。 Path provides an array which includes every element in ascending order from the element you clicked to the top of the DOM. Path提供了一个数组,其中包含从您单击的元素到DOM顶部的每个元素的升序。

Having trouble working out the exact browser support for this though. 虽然无法确定浏览器的确切支持。

 var children = document.querySelectorAll('[id^="child-"]'), clickEvent = function(event) { console.log(event.path[0]); //prints clicked child element console.log(event.path[1]); //prints parent event.path[1].classList.toggle('row'); //toggles row or column mode of parent event.path[0].classList.toggle('selected'); //toggles color of child }; children.forEach(function(child) { child.addEventListener('click', clickEvent); }); 
 <div id="parent"> <div id="child-1">Child One</div> <div id="child-2">Child Two</div> <div id="child-3">Child Three</div> <div id="child-4">Child Four</div> <div id="child-5">Child Five</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM