<script>
function getElem(selector,results){
results = results || [];
var regE = /^(?:#([\w-]+)|\.([\w-]+)|([\w]+)|(\*))$/,
ex = regE.exec(selector);
if(ex){
// console.log(ex)
if(ex[1]){
getID(ex[1],results);
}else if(ex[2]){
getClass(ex[2],results);
}else if(ex[3]){
getTag(ex[3],results)
}else if(ex[4]){
getTag("*",results)
}
}
return results;
}
function getTag(tag,results){
results = results || [];
results.push.apply(results,document.getElementsByTagName(tag));
return results
}
function getID(id,results){
results = results || [];
results.push(document.getElementById(id));
return results
}
function getClass(classname,results){
results = results || [];
results.push.apply(results,document.getElementsByClassName(classname));
return results
}
function each(arr,fn){
for(var i=0;i<arr.length;i++){
if(fn.call(arr[i],i,arr[i])===false){
break
}
}
}
var list = getElem("span",getElem(".sel"));
console.log(list);
each(list,function(){
this.style.backgroundColor = "pink";
})
</script>