【发布时间】:2022-01-12 00:48:36
【问题描述】:
我成功地从 API 遍历对象/数组,并且能够获取三个参数,作者/标题/描述,并将每个参数的结果放入它们自己的列表中。换句话说,数组中的所有作者都在html中放入一个列表中,所有标题放入一个列表中,所有描述放入一个列表中。
如何对结果进行逻辑分组,例如,对于每本书,将结果作者/标题/描述放在页面上的一个区域...(div?)?
const searchButton = document.getElementById('searchbtn')
const form = document.getElementById('myform')
const author = document.querySelector('.author')
const title = document.querySelector('.title')
const description = document.querySelector('.description')
document.addEventListener("DOMContentLoaded", () => {
searchButton.addEventListener('click', e => {
e.preventDefault()
const inputSearch = document.getElementById('books').value
const bookUrl = ('https://www.googleapis.com/books/v1/volumes?q=' + inputSearch)
fetch(bookUrl)
.then (res => res.json())
.then (obj => {
obj.items.forEach((book, index) => {
const listAuthor = document.createElement('li')
const listTitle = document.createElement('li')
const listDescription = document.createElement('li')
listAuthor.innerText = book.volumeInfo.authors
listTitle.innerText = book.volumeInfo.title
listDescription.innerText = book.volumeInfo.description
author.appendChild(listAuthor)
title.appendChild(listTitle)
description.appendChild(listDescription)
})
form.reset()
})
})
})
【问题讨论】:
-
如果不将您当前的代码视为minimal reproducible example,我们将无法帮助您,并简要说明您在执行此分组时遇到的困难,以及一些示例数据。请看How to Ask
-
我在您发布此评论时添加了我的代码。我怎样才能更清楚地解释我的目标?
-
'对于每本书都将结果作者/标题/描述放入一个区域',所以您想要一个 div(书籍容器)并在该 div 中一一列出书籍?您需要实现什么样的分组??只是在单个 div 中具有相应属性的大量书籍?
{作者}、{标题}、{Desc}
-
我正在做一个非常简单的学生项目。我只需要将结果显示在一页上。我当前的代码从 API 获取信息并将其填充到每个类别的三个单独的列表中。我需要“第 1 本书,作者/标题/描述”,然后是“第 2 本书,作者/标题/描述”。我是初学者,不知道你提出的分组是什么意思。
标签: javascript html