两种获取Class的小JS 第一种: function getByClass(nodeType,className){ var nodes = document.getElementsByTagName(nodeType), ret= []; for(var i=0; i<nodes.length; i++) { if(hasClass(nodes[i],className)) ret.push(nodes[i]); } return ret; } function hasClass(node,className){ var names = node.className.split(//n+/); for(var i =0 ; i<names.length; i++){ if(names[i] == className) return true; } return false; } 第二种: var getByClass2 = function(searchClass, node, tag) { if (document.getElementsByClassName) { return document.getElementsByClassName(searchClass) } else { node = node || document; tag = tag || "*"; var classes = searchClass.split(" "), elements = (tag === "*" && node.all) ? node.all : node.getElementsByTagName(tag), patterns = [], returnElements = [], current, match; var i = classes.length; while (--i >= 0) { patterns.push(new RegExp("(^|//s)" + classes[i] + "(//s|$)")); } var j = elements.length; while (--j >= 0) { current = elements[j]; match = false; for (var k = 0, kl = patterns.length; k < kl; k++) { match = patterns[k].test(current.className); if (!match) break; } if (match) returnElements.push(current); } return returnElements; } }