自定义方法通过类名获取对象集合
程序员文章站
2022-03-03 22:33:43
...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<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>
</body>
</html>
上一篇: localstroage的保存用户设置
下一篇: Docker —— 简介与镜像用法