Created
July 26, 2018 19:24
-
-
Save drawcode/b19e0dd1af37aa7012c8d6535767c6bb to your computer and use it in GitHub Desktop.
native-events-js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
div { | |
padding:1em; | |
margin:1em; | |
background:#dadada; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="hasOnClick"> | |
Has onclick | |
</div> | |
<div class="foo"> | |
No onclick | |
</div> | |
<div class="hasOnClick"> | |
<span>Has onclick</span> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const delegateEventListener = ( event, selector, fn ) => document.addEventListener( event, e => { | |
const elements = [...document.querySelectorAll( selector )]; | |
if( elements.includes( e.target ) || elements.some( element => element.contains( e.target ) )){ | |
fn(e); | |
} | |
}); | |
delegateEventListener("click", "div.hasOnClick", e => { | |
console.log( e ); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment