This function allows element methods to be used on nodelists, kinda like what jQuery does.
-
-
Save williammalo/2847249 to your computer and use it in GitHub Desktop.
element methods on nodelists
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
Object.getOwnPropertyNames(Element.prototype).forEach(function(a){ //for each element method | |
NodeList.prototype[a]=function(){ //make a nodelist method | |
for(var n=this.length;n--;)this[n][a].apply(this[n],arguments) //which wraps the element method in a for/in loop that applies the method to every element in the nodelist | |
;return this} | |
}); |
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
Object.getOwnPropertyNames(Element.prototype).forEach(function(a){NodeList.prototype[a]=function(){for(var n=this.length;n--;)this[n][a].apply(this[n],arguments);return this}}) |
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
{ | |
"name": "NodeListMethodizr", | |
"description": "Allows element methods to be used on nodelists", | |
"keywords": [ | |
"Nodelist", | |
"Element", | |
"Method", | |
"JQuery" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>Foo</title> | |
<i>foo bar lorem ipsum</i> | |
<i>foo bar lorem ipsum</i> | |
<i>foo bar lorem ipsum</i> | |
<i>foo bar lorem ipsum</i> | |
<i>foo bar lorem ipsum</i> | |
<i>foo bar lorem ipsum</i> | |
<script> | |
Element.prototype.css=function(a,i,n){for(n in''+a===a||a)this.style[n]=a[n];return i||n?(this.style[a]=i,this):getComputedStyle(this)[a]}; | |
Object.getOwnPropertyNames(Element.prototype).forEach(function(a){NodeList.prototype[a]=function(){for(var n=this.length;n--;)this[n][a].apply(this[n],arguments);return this}}) | |
document.querySelectorAll("i").css({color:"red"}).css({color:"blue"}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment