Created
January 19, 2011 12:18
-
-
Save eAmin/786092 to your computer and use it in GitHub Desktop.
Very Simple CSS Selector
This file contains hidden or 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
/*! | |
* Simple CSS Selector | |
* Copyright(C) 2010 by Amin Akbari [ eAmin.js on gmail ] | |
* Licenced under the MIT Style License [http://www.opensource.org/licenses/mit-license.php] | |
* Created on: 1/22/2010 | |
*/ | |
var selector = function(query) { | |
var trim = function(arg) { return arg.replace(/^\s+|\s+$/, ''); }; | |
var contains = function(arg1, arg2) { return arg1.indexOf(arg2) !== -1; }; | |
query = trim(query); | |
var tag = document.getElementsByTagName('*'), length = tag.length, match = [], seperate = query.split(/,/g); | |
for (var j = 0, leng = seperate.length; j < leng; j++) { | |
var ele = trim(seperate[j]); | |
if (contains(ele, '#')) { | |
for (var i = 0; i < length; i++) { | |
if(tag[i].id === ele.slice(1)) match.push(tag[i]); | |
} | |
} else if (contains(ele, '.')) { | |
for (var i = 0; i < length; i++) { | |
if (contains(' '+tag[i].className+' ', ' '+ele.slice(1)+' ')) | |
match.push(tag[i]); | |
} | |
} else { | |
for (var i = 0; i < length; i++ ) { | |
if (tag[i].nodeName.toLowerCase() === ele) match.push(tag[i]); | |
} | |
} | |
} | |
return match; | |
} |
This file contains hidden or 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
window.onload = function() { | |
var elems = selector('h1, #ids, .class1, p, span'); | |
for (var i = 0; i < elems.length; i++) { | |
elems[i].style.color = '#8977FF'; | |
elems[i].style.fontFamily = 'verdana'; | |
elems[i].style.fontSize = '12px'; | |
elems[i].onmouseover = function() { | |
this.style.color = '#FFFFFF'; | |
this.style.backgroundColor = '#000000'; | |
}; | |
elems[i].onmouseout = function() { | |
this.style.color = '#8977FF'; | |
this.style.backgroundColor = ''; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment