getElementByClass in Javascript - 两种代码-CarlZeng
/* getElementByClass /**********************/ var allHTMLTags = new Array(); function getElementByClass(theClass) { var allHTMLTags=document.getElementsByTagName(‘*‘); for (i=0; i …
/* 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’;