Skip to content

Instantly share code, notes, and snippets.

@rupertqin
Created February 24, 2018 12:11
Show Gist options
  • Save rupertqin/e548c7db4b4b092c1dfd26166e17d1e6 to your computer and use it in GitHub Desktop.
Save rupertqin/e548c7db4b4b092c1dfd26166e17d1e6 to your computer and use it in GitHub Desktop.
var $ = document.querySelectorAll.bind(document)
var $$ = document.querySelector.bind(document)
Element.prototype.on = Element.prototype.addEventListener;
NodeList.prototype.on = function (event, fn) {
[]['forEach'].call(this, function (el) {
el.on(event, fn);
});
return this;
};
Element.prototype.hasClassName = function (a) {
return new RegExp("(?:^|\\s+)" + a + "(?:\\s+|$)").test(this.className);
};
Element.prototype.addClassName = function (a) {
if (!this.hasClassName(a)) {
this.className = [this.className, a].join(" ");
}
};
Element.prototype.removeClassName = function (b) {
if (this.hasClassName(b)) {
var a = this.className;
this.className = a.replace(new RegExp("(?:^|\\s+)" + b + "(?:\\s+|$)", "g"), " ");
}
};
Element.prototype.toggleClassName = function (a) {
this[this.hasClassName(a) ? "removeClassName" : "addClassName"](a);
};
// el loop
[]['some' | 'map' | 'forEach'].call($('.menu a'), function(el) {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment