兼容瀏覽器的getElementsByClassName方法

------但願你們指正錯誤,深表感謝html

<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div class="ss" id="demo"></div> <span class="ss"></span> <div class="sss"></div>  <script>  window.onload = function(){  function getClass(classStr){   if(document.getElementsByClassName){   return document.getElementsByClassName(classStr);   }else{   var nodes = document.getElementsByTagName('*');  var results = [];   for(var i = 0;i<nodes.length;i++){   if(hasClassName(nodes[i],classStr)){  results.push(nodes[i]);  }   }   return results;  }   function hasClassName(nodeStr,classStr){   var arrClassName = nodeStr.className.split(/\s+/);   for(var i = 0;i<arrClassName.length;i++){   if(arrClassName[i] == classStr){  return true;  }   }   return false;   }   }   console.log(getClass('ss'));  }   </script> </body> </html>
相關文章
相關標籤/搜索