-
-
Save justinbmeyer/1202090 to your computer and use it in GitHub Desktop.
my$
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
<html> | |
<head> | |
</head> | |
<body> | |
<ul> | |
<li><a href='#lobsterroll'>Lobster Roll</a></li> | |
<li><a href='#cookies'>Cookies</a></li> | |
<li><a href='#crab'>Crab</a></li> | |
</ul> | |
<div id='lobsterroll'> | |
Lobster Roll | |
</div> | |
<div id='cookies'> | |
Cookies | |
</div> | |
<div id='crab'> | |
Crab | |
</div> | |
<script type='text/javascript' src='jquery-1.6.3.js'></script> | |
<script type='text/javascript' src='hello.js'></script> | |
</body> | |
</html> |
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
// new my$('h1').html("Justin was <span>here</span>").find('span').text() | |
my$ = function(selector){ | |
var elements = selector; | |
if(typeof selector === 'string'){ | |
elements = document.querySelectorAll(selector); | |
} else if(selector.length){ | |
} | |
this.length = 0; | |
[].push.apply(this, my$.makeArray( elements ) ) | |
} | |
var pluginHelper = function(name, getter, setter){ | |
my$.prototype[name] = function(newValue){ | |
if(newValue === undefined){ | |
return getter(this[0]) | |
} else { | |
for(var i =0; i < this.length; i++){ | |
setter(this[i], newValue); | |
} | |
return this; | |
} | |
} | |
}; | |
my$.makeArray = function(arr){ | |
var real = []; | |
for(var i =0; i < arr.length; i++){ | |
real[i] = arr[i] | |
} | |
return real; | |
} | |
pluginHelper('html', | |
function(node){ | |
return node.innerHTML; | |
}, | |
function(node, newValue){ | |
node.innerHTML = newValue; | |
}) | |
pluginHelper('val', | |
function(node){ | |
return node.value; | |
}, | |
function(node, newValue){ | |
node.value = newValue; | |
}) | |
pluginHelper('text', | |
function(node){ | |
return getText(node); | |
}, | |
function(node, newValue){ | |
node.innerHTML = ""; | |
node.appendChild(document.createTextNode(newValue)); | |
}) | |
var getText = function(element){ | |
var txt = ""; | |
for(var i=0; i < element.childNodes.length; i++){ | |
var child = element.childNodes[i]; | |
if(child.nodeType === 1){ | |
txt += getText(child) | |
} else { | |
txt += child.nodeValue | |
} | |
} | |
return txt; | |
} | |
my$.prototype.find = function(selector){ | |
return new my$(this[0].querySelectorAll(selector)) | |
} | |
my$.prototype.children = function(){ | |
return new my$(this[0].children) | |
} | |
my$.prototype.attr = function(name, value){ | |
if(value === undefined){ | |
return this[0].getAttribute(name) | |
} else { | |
for(var i =0; i < this.length; i++){ | |
this[i].setAttribute(name, value) | |
} | |
return this; | |
} | |
} | |
my$.prototype.css = function(name, value){ | |
if(value === undefined){ | |
return document | |
.defaultView | |
.getComputedStyle( this[0], null ) | |
.getPropertyValue( name ) | |
} else { | |
for(var i =0; i < this.length; i++){ | |
this[i].style[name] = value | |
} | |
return this; | |
} | |
} | |
my$.prototype.bind = function(ev, callback){ | |
for(var i =0; i < this.length; i++){ | |
this[i].addEventListener(ev, callback, false) | |
} | |
} | |
my$.prototype.unbind = function(ev, callback){ | |
for(var i =0; i < this.length; i++){ | |
this[i].removeEventListener(ev, callback, false) | |
} | |
} | |
an example for training. A mini jQuery.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm. Kinda looks like a mini-jQuery for modern browsers. What is this?