Home >Web Front-end >JS Tutorial >javascript通过class来获取元素实现代码_javascript技巧

javascript通过class来获取元素实现代码_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:41:56910browse
复制代码 代码如下:

function getByClass(classname){
var nodes = document.getElementsByTagName('*'),
ret=[];
for(var i=0;iif(hasClass(nodes[i],classname))
ret.push(nodes[i]);
}
return ret;
}
function hasClass(node,className){
var names = node.className.split(/\s+/);
for(var i=0;iif(names[i]==className)
return true;
}return false;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn