原生JS和JQ把数组遍历到页面元素

把数组里的值依次放到span中去!

html如下:


<div class='b'><span></span></div>
<div class='b'><span></span></div>
<div class='b'><span></span></div>
<div class='b'><span></span></div>
<div class='b'><span></span></div>
<div class='b'><span></span></div>

js如下:

<script>
    var a = ['1','2','3','4','5','6'];
    var b = document.querySelectorAll(".b span");
    a.forEach(function(v, i){
        b[i].innerHTML = v;
    });
</script>

jq如下:

<script>
    var a = ['1','2','3','4','5','6'];
    $('.b span').each(function(index,element){
        $(element).html(a[index])
    })
</script>
经验jsJQuery数组遍历

我来吐槽

*

*