Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 17:41:56
Original
909 people have browsed it
复制代码 代码如下:

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;
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template