自定义方法通过类名获取对象集合
程序员文章站
2022-03-24 09:31:30
...
图片展示
代码展示
<div class="one">我是one</div>
<div class="wrap">
<div class="one">我也是one</div>
<div class="two">我也two</div>
<div class="one">i'm one too</div>
</div>
<script>
function selectorClass(className) {
if (className.indexOf('.') === 0) {
className = className.slice(1);
}
return document.getElementsByClassName(className);
}
var oneObjArr = selectorClass('.one');
console.log(oneObjArr);
for (var i in oneObjArr) {
oneObjArr[i].innerHTML = '我们都是one'
}
</script>