自定义方法通过类名获取对象集合
发布于:2021-12-10 08:57:21 栏目:技术文档
图片展示
代码展示
<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>
相关推荐
阅读 +