getElementsByClassName

<script type="text/javascript">
   function  ele(){
    /* var elements = getElementsByClassName(document," ace_editor ace-chrome"); */
    var elements = getElementsByClass("div"," ace_editor ace-chrome");
     var ele1 =elements[0];
     console.log("shhd"+ele1);
     ele1.style.height="260px";
   }
   function getElementsByClass(tagName,cName) {
   var elements = tagName ? document.getElementsByTagName(tagName) : document.getElementsByTagName('*');
         var findEles = [];
         var reg = new RegExp('^'+cName+'\\s*|\\s+'+cName+'\\s+|\\s+'+cName+'$');
         for(var i=0;i<elements.length;i++) {
             if(reg.test(elements[i].className))findEles.push(elements[i]);          
         }
         return findEles;
 }
  function getElementsByClassName(node,classname) {
   if (node.getElementsByClassName) { // use native implementation if available
     return node.getElementsByClassName(classname);
   } else {
     return (function getElementsByClass(searchClass,node) {
         if ( node == null )
           node = document;
         var classElements = [],
             els = node.getElementsByTagName("*"),
             elsLen = els.length,
             pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;
 
         for (i = 0, j = 0; i < elsLen; i++) {
           if ( pattern.test(els[i].className) ) {
               classElements[j] = els[i];
               j++;
           }
         }
         return classElements;
     })(classname, node);
   }
 }
  </script>

你可能感兴趣的:(getElementsByClassName)