域名已变更 请手动修改文章中域名指向carlzeng.com

getElementByClass in Javascript - 两种代码

/* getElementByClass
    /**********************/
    var allHTMLTags = new Array();

    function getElementByClass(theClass)

 {
    
    var allHTMLTags=document.getElementsByTagName('*');
    for (i=0; i
    if (allHTMLTags[i].className==theClass) {
    allHTMLTags[i].style.display=’none’;

    }
    }
    }



-----------------------------方法二.

document.getElementByClass = function (classname) {
   var elements = [];
   var alltags = document.all ? document.all : document.getElementsByTagName("*")
   for (var i=0; i
      var classNames = alltags[i].className.split(" ");
      if (classNames.contain(classname))
          elements[elements.length] = alltags[i];
   }
   return elements;
}

==================应用:
var Tags = new Array();
var j=0;
var theClass='machineheadernosort';
var allHTMLTags=document.getElementsByTagName('*');
    for (var i=0; i<allHTMLTags.length; i++)
    {
    if (allHTMLTags[i].className==theClass) {
    //allHTMLTags[i].style.display='none';
    Tags[j]=allHTMLTags[i];
    j++;
    }
    }
Tags[2].style.display='none';
posted @ 2008-10-07 23:19  CarlZeng  阅读(3284)  评论(0编辑  收藏  举报
域名已变更 请手动修改文章中域名指向carlzeng.com