Created
October 31, 2012 22:42
-
-
Save oslego/3990424 to your computer and use it in GitHub Desktop.
Small jquery-like experiment
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
var $ = function(){ | |
function noQuery() { | |
var args = Array.prototype.slice.call(arguments, 0)[0], | |
query = args[0] | |
qsa = function(q){ | |
return Array.prototype.slice.call(document.querySelectorAll(q), 0) | |
} | |
this.el = [] | |
if (typeof query == 'string'){ | |
// create element | |
if (/^<([a-z])+>$/.test(query)){ | |
var el = document.createElement(query.substr(1, query.length-2)) | |
this.el.push(el) | |
} | |
// query selector | |
else{ | |
this.el = qsa(query) | |
} | |
} | |
} | |
noQuery.prototype.each = function(arr, iterator){ | |
arr.forEach(iterator) | |
} | |
noQuery.prototype.on = function(ev, fn){ | |
this.each(this.el, function(el){ | |
el.addEventListener(ev, fn) | |
}) | |
return this | |
} | |
noQuery.prototype.off = function(ev, fn) { | |
this.each(this.el, function(el){ | |
el.removeEventListener(ev, fn) | |
}) | |
return this | |
} | |
noQuery.prototype.css = function(prop, value){ | |
this.el.forEach(function(el){ | |
}) | |
return this | |
} | |
noQuery.prototype.attr = function(prop, value){ | |
if (arguments.length == 1){ | |
if (typeof prop == 'string'){ | |
this.each(this.el, function(el){ | |
return el.getAttribute(prop) | |
}) | |
} | |
} | |
return this | |
} | |
return new noQuery(arguments) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment