简体   繁体   English

如何将多个 JavaScript 对象数组填充到 HTMLDOM

[英]How to populate multiple JavaScript arrays of objects to HTMLDOM

I am having difficulty to get all the array of objects and display it into HTML lists.我很难获取所有对象数组并将其显示到 HTML 列表中。 Can anyone help me, please.任何人都可以帮助我,请。 The below is my HTML and JavaScript code.下面是我的 HTML 和 JavaScript 代码。 Looking forward to your help.期待您的帮助。

 const allData = [{ date: '2nd', venue: 'venue1', location: 'location1', }, { date: '3rd', venue: 'venue2', location: 'location2', }, { date: '4th', venue: 'venue3', location: 'location3', } ] allData.forEach(data => { [...document.querySelectorAll('.targets')].forEach(list => { list.innerHTML = ` <h5 >DATE</h5> <h4 >${data.date}</h4> <h5 >VENUE</h5> <h4 >${data.venue}</h4> <h5 >LOCATION</h5> <h4 >${data.location}</h4> <Button >BUY TICKETS</Button> `; }) });
 <ul> <li class="targets"></li> </ul>

If you change the order of for loops execution and append each string to the previous it works!如果您更改 for 循环执行的顺序并将每个字符串附加到前一个字符串,它就可以工作!

 const allData = [{ date: '2nd', venue: 'venue1', location: 'location1', }, { date: '3rd', venue: 'venue2', location: 'location2', }, { date: '4th', venue: 'venue3', location: 'location3', }, ]; const list = document.querySelector('.target') let innerHTML = ''; allData.forEach(data => { innerHTML += ` <li> <h5 class = "shows__date">DATE</h5> <h4 class = "shows__calander">${data.date}</h4> <h5 class = "shows__venue-title">VENUE</h5> <h4 class = "shows__venue">${data.venue}</h4> <h5 class = "shows__location-title">LOCATION</h5> <h4 class = "shows__location">${data.location}</h4> <Button Class = "shows__btn">BUY TICKETS</Button> </li> `; }); list.innerHTML = innerHTML;
 <ul class="target"> </ul>

I think you don't need to loop for class='targets' because you only have one li in your html code.我认为您不需要为class='targets'循环,因为您的html代码中只有一个li It might be better to just get the ul element and then loop allData variable, then change the ul innerHTML on each loop.最好只获取ul元素,然后循环allData变量,然后在每个循环中更改ul innerHTML

HTML Code HTML代码

<ul></ul>

JS Code: JS代码:

const allData= [
{
   date:  '2nd',
   venue: 'venue1',
   location: 'location1',

},

{
    date:  '3rd',
    venue: 'venue2',
    location: 'location2',
    
 },

 {
    date:  '4th',
    venue: 'venue3',
    location: 'location3',
    
 },
]

let ul = document.querySelector('ul')
let listContent = ''
allData.forEach(data=>{
        listContent = listContent +
          `                  
        <li>
            <h5 >DATE</h5>
            <h4 >${data.date}</h4>
            <h5 >VENUE</h5>
            <h4 >${data.venue}</h4>
            <h5 >LOCATION</h5>
            <h4 >${data.location}</h4>
            <Button >BUY TICKETS</Button>
        </li>
        `;
   });

   ul.innerHTML = listContent

Edited based on pilchard comment根据pilchard评论编辑

The OP provides a basic list structure by the "naked" <ul/> / <li/> markup. OP 通过“裸” <ul/> / <li/>标记提供基本列表结构。

Thus, there is only a sole <li class="targets"></li> element which can be accessed with a query like '.targets' .因此,只有一个<li class="targets"></li>元素可以通过像'.targets'这样的查询来访问。 Which means, the OP always writes to one and the same element which shows the expected result of a list which features just one element with the data-array's last item-data.这意味着,OP 总是写入一个相同的元素,该元素显示列表的预期结果,该列表仅包含一个元素和数据数组的最后一项数据。

But the <li/> element can be used as a blueprint for creating other list-item elements via<node>.cloneNode which all will be <li class="targets"></li> -alike.但是<li/>元素可以用作通过<node>.cloneNode创建其他列表项元素的蓝图,这些元素都将与<li class="targets"></li>类似。

Now one can assign the correct data-item related html content to each newly created list-item clone which also gets append ed to its parent list-element ...现在可以将正确的数据项相关的 html 内容分配给每个新创建的列表项克隆,这些克隆也附加到其父列表元素...

 const allData = [{ date: '2nd', venue: 'venue1', location: 'location1', }, { date: '3rd', venue: 'venue2', location: 'location2', }, { date: '4th', venue: 'venue3', location: 'location3', }]; const venueItemBlueprint = document.querySelector('li.targets'); const venueList = venueItemBlueprint && venueItemBlueprint.parentElement; if (venueList) { venueList.innerHTML = ''; allData.forEach(venueData => { const venueItem = venueItemBlueprint.cloneNode(); venueItem.innerHTML = ` <h5>DATE</h5> <h4>${ venueData.date }</h4> <h5>VENUE</h5> <h4>${ venueData.venue }</h4> <h5>LOCATION</h5> <h4>${ venueData.location }</h4> <Button>BUY TICKETS</Button>`; venueList.appendChild(venueItem); }); }
 <ul> <li class="targets"></li> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM