var parents = document.getElementsByClassName("someClass"),
forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach),
filter = Array.prototype.filter.call.bind(Array.prototype.filter)
forEach(parents, addClassToEveryThirdChild)
function addClassToEveryThirdChild(parent) {
filter(parent.children, selectEveryThirdChild)
.forEach(addSomeClass)
}
function selectEveryThirdChild(elem, i) {
return i % 3 === 0
}
function addSomeClass(elem) {
elem.classList.add("newClassName")
}
或带循环
var parents = document.getElementsByClassName("someClass")
for (var i = 0, ii = parents.length; i < ii; i++) {
var parent = parents[i],
children = parent.children
for (var j = 0, jj = children.length; j < jj; j++) {
var elem = children[j]
if (j % 3 === 0) {
elem.classList.add("newClassName")
}
}
}