自定义方法通过类名获取对象集合
程序员文章站
2022-03-13 13:56:04
...
<!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>
上一篇: JavaScript制作日历
下一篇: 纯真破解工作室