getElementsByClass(2)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>An XHTML 1.0 Strict standard template</title> <meta http-equiv="content-type" content="text/html;charset=gbk" /> <script type="text/javascript"> function getElementsByClass(node,searchClass,tag) { var classElements = new Array(); if ( node == null ) node = document; if ( tag == null ) tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); for (i = 0, j = 0; i < elsLen; i++) { if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; } } return classElements; } </script> </head> <body> <div class="foo"> 在做完getElementsByClass的测试时,有点困惑。就是不能像用getElementsById()那样的随便,但是它也能够很好地工作,像它们一样。<p/> </div> <script type="text/javascript"> var oO=getElementsByClass(document,'foo','*'); var p=(oO.background="#f00"); //可以直接访问或设置属性 alert(p); </script> </body> </html>