//后两参数是可靠的,查找一网页中5007个类名为“cell”的元素,IE8历时78毫秒,IE6历时125~171毫秒 //FF3.5为42 ~ 48毫秒,opera10为31 毫秒,Chrome为22~ 25毫秒,safari4为18 ~ 19毫秒 var getElementsByClass = function(searchClass,node,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; }
function test(){ document.getElementById("test").onmouseover=function(){document.getElementById("test2").style.backgroundColor="red"}; document.getElementById("test").onmouseout=function(){document.getElementById("test2").style.backgroundColor="blue"}; }
//弹出文档中name为test的元素的个数
function test() { var test=document.getElementsByName("test"); alert(test.length); }