Created
August 25, 2012 21:02
-
-
Save jaeschrich/3470957 to your computer and use it in GitHub Desktop.
Nano.js. A 25-line jQuery impersonator. Incomplete version
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(s){ // Makes the main function (accepts one value) | |
var sel = s.substring(1,s.length) // Get's the selector text minus the first character (like #) | |
switch (s.charAt(0)){ // Switch based on first chararcter | |
case '#': // If it's a #, do document.getElementById | |
return document.getElementById(sel) | |
break; | |
case '.': // If it's a ., get the element by class name | |
var allEls = document.getElementsByTagName('*') | |
for (var i = 0;i<allEls.length;i++){ | |
if (allEls[i].className === sel){ | |
return allEls[i] | |
} | |
} | |
break; | |
case '*': // If a star, do document.getElementByTagName | |
return document.getElementsByTagName(sel) | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment