通过获取class获取元素

function getByClass(oParent,sClass){

  if(oParent.getElementsByClassName){

return oParent.getElementsByClassName(sClass);

}else{//IE低版本

var aElt = oParent.getElementsByTagName("*");

var arr = [];

for(var i = 0;i<aElt[i].className.split(' ');

if(findInArray(sClass,arr2)){

arr.push(aElt[i]);

}

}

return arr;

}

}

//正则版

function getByClass(oParent,sClass){

if(oParent.getElementsByClassName){

return  oParent.getElementsByClassName(sClass);

}else{

var tagArr = oParent.getElementsByTagName('*');

var tagLen = tagArr.length;

var arr = [];

var re = new RegExp('\\b'+sClass+'\\b');

for(var i = 0;i < tagLen;i++){

if(re.test(tagArr[i].className)){

arr.push(tagArr[i]);

}

}

return arr;

}

}

推荐阅读更多精彩内容