JavaScript -循环创建卡片列表

fv2wmkja  于 2023-06-28  发布在  Java
关注(0)|答案(4)|浏览(178)

HTML和简单的JS(没有react / vue)。我需要做一个列表卡,并希望做一个循环,以避免重复的html。
当前代码:

<div id="products-cards-container">
    <div class="products-cards">              
        <div class="product-header">
            <img src="../assets/img/image1.png"/>
        </div>
        <div class="product-content">
            <h4>title 1</h4>
            <p>super content 1</p>
        </div> 
        <button class="info-button">+ info</button>
    </div>
    <div>
        <div class="product-header">
            <img src="../assets/img/cards/products/image2.png"/>
        </div>
        <div class="product-content">
            <h4>title 2</h4>
            <p>super content 2</p>
        </div> 
        <button class="info-button">+ info</button>
    </div>
    <div>
        <div class="product-header">
            <img src="../assets/img/cards/products/image-3.png"/>
        </div>
        <div class="product-content">
            <h4>title 3</h4>
            <p>blablablablbalbalbabla blablaba</p>
        </div> 
        <button class="info-button">+ info</button>
    </div>\
</div>

我正在尝试返回html
script.js

const valuesCards = [
    { 
    image: '../img/image1.png', 
    title: 'title 1', 
    content: 'super content 1',
    },
    {
    image: '../img/image2.png', 
    title: 'title 2', 
    content: 'super content 2'
    },
    { 
    image: '../img/image-3.png', 
    title: 'title3', 
    content: 'blablablablbalbalbabla blablaba'
    },
   ]

创建一个函数,将卡片列表插入.products-cards div中:

function returnCards() => {
    ----- AND HERE I AM STUCK
   (as well, all tries I did with a simple array / object returned only the last info) ----
    }
0h4hbjxa

0h4hbjxa1#

使用Array.prototype.map函数和模板文字。

const container = document.getElementById('products-cards-container');
const valuesCards = [{
    image: '../img/image1.png',
    title: 'title 1',
    content: 'super content 1',
  },
  {
    image: '../img/image2.png',
    title: 'title 2',
    content: 'super content 2'
  },
  {
    image: '../img/image-3.png',
    title: 'title3',
    content: 'blablablablbalbalbabla blablaba'
  },
]

function returnCards(valuesCards) {
  return "<div class=\"products-cards\">" + valuesCards.map(valuesCard => `
  <div>
    <div class="product-header">
      <img src="${valuesCard.image}"/>
    </div>
    <div class="product-content">
      <h4>${valuesCard.title}</h4>
      <p>${valuesCard.content}</p>
    </div> 
    <button class="info-button">+ info</button>
  </div>`).join('') + "</div>";
}

container.innerHTML = returnCards(valuesCards);
<div id="products-cards-container"></div>
kx1ctssn

kx1ctssn2#

你可以这样做

const valuesCards = [
    { 
    image: '../img/image1.png', 
    title: 'title 1', 
    content: 'super content 1',
    },
    {
    image: '../img/image2.png', 
    title: 'title 2', 
    content: 'super content 2'
    },
    { 
    image: '../img/image-3.png', 
    title: 'title3', 
    content: 'blablablablbalbalbabla blablaba'
    },
   ];
   
   let cardHTML = '';
   valuesCards.map(element => {
        cardHTML += '<div> \
        <div class="product-header"> \
         <img src="'+element.image+'"/> \
        </div> \
        <div class="product-content"> \
           <h4>'+element.title+'</h4> \
         <p>'+element.content+'</p> \
        </div> \
        <button class="info-button">+ info</button> \
      </div> \
      ';
   });
   
document.getElementsByClassName('products-cards')[0].innerHTML = cardHTML;
<div id="products-cards-container">
       <div class="products-cards"> 
         
       </div>
</div>
bd1hkmkf

bd1hkmkf3#

const valuesCards = [
    { 
    image: '../img/image1.png', 
    title: 'title 1', 
    content: 'super content 1',
    },
    {
    image: '../img/image2.png', 
    title: 'title 2', 
    content: 'super content 2'
    },
    { 
    image: '../img/image-3.png', 
    title: 'title3', 
    content: 'blablablablbalbalbabla blablaba'
    },
   ]
valuesCards.map(card=> {
  var cardDiv = document.createElement('div');
  cardDiv.innerHTML = `
                <div class="product-header">
                  <img src="${card.image}"/>
                </div>
                <div class="product-content">
                  <h4>${card.title}</h4>
                  <p>${card.content}</p>
                </div> 
                <button class="info-button">+ info</button>`
  document.getElementsByClassName('products-cards')[0].appendChild(cardDiv);
})
<div id="products-cards-container">
       <div class="products-cards">  
          </div>
</div>
mwkjh3gx

mwkjh3gx4#

您需要执行一个“for循环”,迭代valuesCards对象,并为每个值返回一个html模板,然后注入到DOM中所需的元素中。
例如:

// Define the object
    const valuesCards = [
      {
        'image': '../img/image1.png',
        'title': 'title 1',
        'content': 'super content 1',
      },
      {
        'image': '../img/image2.png',
        'title': 'title 2',
        'content': 'super content 2'
      },
      {
        'image': '../img/image-3.png',
        'title': 'title3',
        'content': 'blablablablbalbalbabla blablaba'
      }
    ]
    
    // Identify the DOM element to store cards
    cardsContainer = document.querySelector('#products-cards-container');
    
    // Create a loop that iterates over valuesCards object
    for (let value of Object.values(valuesCards)) {
      console.log(value.title);
        // Create an element to store new card content
        let newCard = document.createElement('DIV');
    
        // Add products-cards class to new element
        newCard.classList.add('products-cards');
    
        // Create a multiline string template with current values each time 
        cardHtml = `
           <div class="">              
            <div class="product-header">
                <img src="${value.image}"/>
            </div>
            <div class="product-content">
                <h4>${value.title}</h4>
                <p>${value.content}</p>
            </div> 
            <button class="info-button">+ info</button>
           </div>   
        `;
    
        // Append the new element to the cardsContainer element in DOM
        cardsContainer.appendChild(newCard);
    
        // Inject the template html on DOM's new append item
        newCard.innerHTML = cardHtml;
    
    }

重要提示!!在要打印卡片的html中创建id=“products-cards-container”的div元素。

相关问题