亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用ES6純Javascript將路徑數組中的html集合(img)的src屬性設置為在每個

使用ES6純Javascript將路徑數組中的html集合(img)的src屬性設置為在每個

Qyouu 2022-09-02 16:20:56
我有一個src值數組,如[path1,path2,path3]和一個與arr值相同的數組長度的html集合。因此,我想傳遞每個值來設置html元素的src。我該怎么辦?我嘗試了 forEach,for Of,map,但它仍然是同一路徑中的所有元素,或者只是設置了一個(第一個元素)。//My code now<img alt='first-in-collection'><img alt='second-in-collection'><img alt='third-in-collection'> //The variablelet arr = ['path1', 'path2', 'path3']//Expected result <img src='path1' alt='first-in-collection'><img src='path2' alt='second-in-collection'><img src='path3' alt='third-in-collection'>
查看完整描述

1 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

無需同時在映像數組和路徑數組上運行循環。在任何一個陣列上運行循環。繼續遞增迭代器計數,并設置當前迭代器索引圖像的 src。


let arr = ['path1', 'path2', 'path3'];\

// get all images

let img = document.getElementsByClassName('img-thumbnail')

// initialize iterator with 0    

let i=0;     

for(let el of arr){

       // set attribute of corresponding image

        img[i++].setAttribute('src', el)

        

    }

<div class="col-8">

    <div class="row produit-desc-lense">

        <div class="col-4 text-center border-bottom">

            <img class="img-fluid rounded img-thumbnail" alt="/">

            <h5>caméra 2</h5>

        </div>

        <div class="col-4 d-flex align-items-center justify-content-center border-bottom">

            <p></p>

        </div>

        <div class="col-4 d-flex align-items-center justify-content-center border-bottom">

            <select id="selectNumber">

                <option>Choose a number</option>

            </select>

        </div>

    </div><div class="row produit-desc-lense">

        <div class="col-4 text-center border-bottom">

            <img class="img-fluid rounded img-thumbnail" alt="/">

            <h5>caméra 2</h5>

        </div>

        <div class="col-4 d-flex align-items-center justify-content-center border-bottom">

            <p></p>

        </div>

        <div class="col-4 d-flex align-items-center justify-content-center border-bottom">

            <select id="selectNumber">

                <option>Choose a number</option>

            </select>

        </div>

    </div><div class="row produit-desc-lense">

        <div class="col-4 text-center border-bottom">

            <img class="img-fluid rounded img-thumbnail" alt="/">

            <h5>caméra 2</h5>

        </div>

        <div class="col-4 d-flex align-items-center justify-content-center border-bottom">

            <p></p>

        </div>

        <div class="col-4 d-flex align-items-center justify-content-center border-bottom">

            <select id="selectNumber">

                <option>Choose a number</option>

            </select>

        </div>

    </div>

    

</div>


查看完整回答
反對 回復 2022-09-02
  • 1 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號