Created
January 23, 2017 06:10
-
-
Save mach3/83dd1c50288ca5eb9a8e2f6574ecc754 to your computer and use it in GitHub Desktop.
$.fn.on(eventType, selector, selector, handler) みたいな物を。挙動はちょっと異なります。
This file contains hidden or 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
var _delegate = function(elms, type, selector, callback){ | |
elms = (_type(elms) === "string") ? document.querySelectorAll(elms) | |
: (! elms.length) ? [elms] | |
: elms; | |
Array.from(elms).forEach(function(elm){ | |
elm.addEventListener(type, function(e){ | |
var valid = !! Array.from(this.querySelectorAll(selector)) | |
.filter(function(el){ | |
return el === e.target; | |
}).length; | |
if(valid){ | |
callback.call(this, e); | |
} | |
}, false); | |
}); | |
}; |
This file contains hidden or 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
_delegate("#container", "click", ".childItem", function(e){ | |
console.log(e.target); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment