Quantcast
Channel: w3cplus
Viewing all articles
Browse latest Browse all 1557

纯js通过class选择元素

$
0
0

高级浏览器已经支持了getElementsByClassName,所以如果支持就使用getElementsByClassName,如果不支持则通过循环选择

function getElementsByClassName(node,classname){
  if(node.getElementsByClassName){
    return node.getElementsByClassName(classname);
  }else{
    var results = new Array();
    var elems = node.getElementsByTag("*");
    for (var i=0;i<elems.length;i++){
      if(elems[i].className.indexOf(classname) != -1){
        results[elems.length] = elems[i];
      }
    }
    return results;
  }
}

Viewing all articles
Browse latest Browse all 1557

Trending Articles