lundi 30 avril 2018

HTML MouseEvent, target the element which is added the listener, not its childs [duplicate]

Is there a way to add an event listener to an element and always get an event with itself as a target, not its children?

In this example, you will see that the target depends on which element is being mouse hovered. My expected output would be to have ALWAYS the outer element as the target element, is this possible?

Thanks in advance.

var outer = document.getElementById('outer'),
    inner = document.getElementById('inner');

outer.addEventListener('mousemove',function(event){
   if (event.target === outer) {
        console.log('outer')
   }
   
   if (event.target === inner) {
        console.log('inner')
   }
});
#outer {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 100px;
  height: 100px;
  background: green
}

#inner {
  position: absolute;
  bottom: 0;
  right: 0;
  background: red
}
<div id='outer'>
    Outer Tag
    <div id='inner'>Inner Tag</div>
</div>



Aucun commentaire:

Enregistrer un commentaire