简体   繁体   English

如何使用JavaScript在表格中返回图像数组及其文本内容

[英]How to return array of images and its text content in a table using javascript

I am really new with Javascript and I need help on how to display an array of book cover thumbnails with their titles and prices using a table in Javascript. 我真的是Java语言的新手,我需要有关如何使用Javascript表显示书籍封面缩略图及其标题和价格的帮助。 This is what I have so far. 到目前为止,这就是我所拥有的。

var bookImages = new Array ('images/animals-116x150.jpg', 'images/dog-loves-counting-125x150.jpg', ...);

var bookTitle = new Array ('Animals And Their Families', 'Dog Loves Counting', ...);

var bookDescr = new Array ('...','....');

var authors = new Array ('By Barbara Nascimbeni', 'By Louise Yates', ...);

var coverPrice = new Array ('Hardcover, <b>Price: $5.99 CDN</b></style>; 'Hardcover, <b>Price: $5.99 CDN</b>', ...);

var books = document.getElementById("bookSales");

var a;

var bookContent = '<table>';  *this  part contains open table tag, just not showing for some reason*

for (a = 0; a < bookImages.length; a++) {

    bookContent += "<tr><td><img src='" + bookImages[a] + "</td>";  
    bookContent += "<td>" + "<b>" + bookTitle[a] + "</b>" + "<br>" + bookDescr[a] + "<br>" + authors[a] + "<br>" + coverPrice[a] + 
    "<br><br><br><br><br><br><br><br>" + "</td></tr>"; 
}

bookContent += "</table>"; *this contains end table tag*

books.innerHTML = bookContent;

You should use only one array that will contain object. 您应该只使用一个包含对象的数组。 Each object will represent a book an all its properties. 每个对象将代表一本书及其所有属性。 You can then simply call book.description to get the description of a book while iterating your array 然后,您可以在迭代数组时简单地调用book.description以获得一本书的描述

 let books = [{ image: 'image/A.png', title: 'Mysterious Book', description: 'foo..faa.', authors: ['John McFee', 'Jack McGordon'], coverPrice: 5.99 }, { image: 'image/B.png', title: 'What a Book', description: 'descriptio is life', authors: ['Gipsy Guy'], coverPrice: 10.95 }]; let bookSales = document.getElementById('bookSales'); let list = ""; books.forEach(book => { list += "<tr>"; list += "<td><img src='" + book.image + "'></td>"; list += "<td>" + "<b>" + book.title + "</b>" + "<br>" + book.description + "<br>" + book.authors.join(',') + "<br>" + book.coverPrice + "</td>"; list += "</tr>"; }); bookSales.innerHTML += list; 
 table, th, td { border: 1px solid black; } 
 <table> <tbody id="bookSales"> </tbody> </table> 

暂无
暂无

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

相关问题 如何使用HtmlUnit检索JavaScript变量(数组)及其内容 - How to retrieve javascript variable (an array) and its content using HtmlUnit 如何在Test Cafe中使用Javascript将表作为数组返回 - How to return table as array using Javascript in Test Cafe 如何显示/隐藏<li>基于其使用 JavaScript 的文本内容? - How do I show/hide an <li> based on its text content using JavaScript? 如何使用JavaScript访问表单选择标签的value属性的内容或其内部文本? - How can I acces to the content of the value attribute of a form select tag or to its inner text using JavaScript? 如何使用 javascript 构建文档内容的文本数组 - How to built a text array of the document content with javascript 单击javascript / jquery中的文本后如何启用选项卡及其内容? - How to enable tab and its content on click of a text in javascript/jquery? 如何将本地文本文件转换为数组并使用 javascript 显示其元素 - How to convert a local text file to an array and display its elements using javascript 我正在尝试遍历目录及其子文件夹中的所有文件,获取每个文件的文本内容并返回一个文本内容数组 - I'm trying to loop through all files in directory & its subfolders, get each file's text content & return an array of text content JavaScript-如何在数组中递归地查找元素并返回其路径? - JavaScript- How to find an element and return its path in an array recursively? 如何通过检查其属性和 javascript 中的 arraylist 返回对象数组 - How to return array of objects by checking its property and arraylist in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM