js通过元素class名字获取元素集合的具体实现_javascript技巧
时间:2021-07-01 10:21:17
帮助过:6人阅读
代码如下:
function getElementsByClassName(n) {
var classElements = [],allElements = document.getElementsByTagName('*');
for (var i=0; i< allElements.length; i++ )
{
if (allElements[i].className == n ) {
classElements[classElements.length] = allElements[i];
}
}
return classElements;
}