新版Blog上线,来看看?

星期五, 十月 14, 2005

  Javascript中的getElementById十分常用,但在标准的页面中,一个id只能出现一次,如果我想同时控制多个元素,例如点一个链接, 让多个层隐藏,该怎么做?用class,当然,同一个class是可以允许在页面中重复出现的,那么有没有getElementByClass呢?没有, 但是可以解决:
//Create an array
var allPageTags = new Array();

function hideDivWithClasses(theClass) {
//Populate the array with all the page tags
var allPageTags=document.getElementsByTagName("div");
//Cycle through the tags using a for loop
for (i=0; i//Pick out the tags with our class name
if (allPageTags[i].className==theClass) {
//Manipulate this in whatever way you want
allPageTags[i].style.display='none';
}
}
}
PostBy Post by Dennis Lee at 10/14/2005 05:07:00 下午 |  

评论