Blogger Information
Blog 38
fans 1
comment 0
visits 24343
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5月7日作业 forEach遍历数组
鲨鱼辣椒的博客
Original
560 people have browsed it

用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>
运行实例 »
点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post