js兼容方法:通过样式名获取元素,byClass

时间:2022-01-15 22:19:45
function byClass(oParent,className){
  if(document.getElementsByClassName){ //if it is Firefox
    return oParent.getElementsByClassName(className);
  }else{
    var tags = oParent.getElementsByTagName("*");
    var tagsLength = tags.length;
    var result = [];
    for(var i=0;i<tagsLength;i++){
      if(tags[i].className==className){
        result.push(tags[i]);
      }
    }
    return result;
  }
}