Correction status:Uncorrected
Teacher's comments:
用slice方法把对象转换为数组 再用forEach遍历对象数组
代码如下:
实例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用slice方法将对象转为数组,再用forEach进行遍历</title> </head> <body> <h3>obj1</h3> <h3>obj2</h3> <h3>obj3</h3> <h3>obj4</h3> <h3>obj5</h3> <h3>obj6</h3> <script> var hh = document.getElementsByTagName('h3'); console.log(hh); var arr = Array.prototype.slice.call(hh); arr.forEach(function (value,key) { if ((key % 2)!== 0){ value.style.backgroundColor = 'blue'; } }); </script> </body> </html> 运行实例 » 点击 "运行实例" 按钮查看在线实例