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

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

縮短多重和重復屬性

縮短多重和重復屬性

aluckdog 2021-09-30 17:25:00
(我今天早些時候遇到了類似的問題,但現在不同了)。我有一個代碼來創建一個元素,為它們設置屬性,最后,附加到父 div。我已經試過了:Object.entries(selectorsByProp).forEach(([prop, selector]) => {    document.querySelector(selector).innerHTML = post[i][prop];});這實際上有效,但它只能操作顯示和顯示我的數據的元素。我需要的是它必須創建元素、設置屬性和附加。這是我的可笑代碼...var post = JSON.parse(this.response);for (var i = 0; i < 3; i++) {    var listcard = document.createElement("div"),        imgcontent = document.createElement("div"),        imgcntnr = document.createElement("div"),        imglnk = document.createElement("a"),        a = document.createElement("a"),        img = document.createElement("img"),        txtcntnt = document.createElement("div"),        span = document.createElement("span");    listcard.setAttribute('class', 'list-card');    imgcontent.setAttribute('class', 'img-content');    imgcntnr.setAttribute('class', 'img-container');    imglnk.setAttribute('href', '#');    img.setAttribute('class', 'thumb');    txtcntnt.setAttribute('class', 'text-content');    a.setAttribute('href', '#');    a.setAttribute('class', 'title');    span.setAttribute('class', 'excerpt');    img.setAttribute('src', post[i].img);    img.setAttribute('alt', post[i].titles);    a.innerHTML = post[i].titles;    span.innerHTML = post[i].descs;    imglnk.appendChild(img);    imgcntnr.appendChild(imglnk);    imgcontent.appendChild(imgcntnr);    txtcntnt.innerHTML += a.outerHTML + span.outerHTML;    listcard.innerHTML += imgcontent.outerHTML + txtcntnt.outerHTML;    parent.appendChild(listcard);}<div class="list-card">    <div class="img-content">        <div class="img-container">            <a href="#"> <img src="https://picsum.photos/182/135/?random" alt="" class="thumb"></a>        </div>    </div>    <div class="text-content">        <a href="#" class="title">Lorem Ipsum</a>        <span class="excerpt">Lorem Ipsum</span>    </div></div>
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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