页面代码如下:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>自定义方法,实现通过类名获取对象集合</title></head><body><h1 id='one'>自定义方法,实现通过类名获取对象集合</h1><h2 class='one'>自定义方法,实现通过类名获取对象集合</h2><p class='one'>自定义方法,实现通过类名获取对象集合</p><div class='two one'>自定义方法,实现通过类名获取对象集合</div><h2 class='two'>自定义方法,实现通过类名获取对象集合</h2><h2 class='tree one'>自定义方法,实现通过类名获取对象集合</h2><h2 class='tree two'>自定义方法,实现通过类名获取对象集合</h2><script>var x=document.getElementsByClassName('one'); //x是一个对象集合的数组for(i = 0; i < x.length; i++){x[i].style.background='green';x[i].style.color = 'yellow'}console.log(x)</script></body></html>
结果如下:

相关推荐
© 2020 asciim码
人生就是一场修行