从谷歌图书 api 中分离元素

很久以前我没有用javascript编程所以我决定做一个“书柜”的项目来管理阅读的书籍并且我想阅读更多我对如何分离元素以个性化风格有困难因为它在一个 div 中选择了 api 的所有结果。


html


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="script.js"></script>

    <link rel="stylesheet" href="./css/bookcase.css">

    <title>project</title>

</head>

<body>


    <div id="content">

    </div>


    <script src="https://www.googleapis.com/books/v1/volumes?q=clean+code&callback=handleResponse></script>

</body>

</html>

js


function handleResponse(response) {

    for (var i = 0; i < response.items.length; i++) {

      var item = response.items[i];

      var book = document.getElementById('content')

      book.innerHTML += "<br>" + '<img src=' + response.items[i].volumeInfo.imageLinks.thumbnail + '>';

      book..innerHTML += "<br>" + item.volumeInfo.title;

      book..innerHTML += "<br>" + item.volumeInfo.authors;


冉冉说
浏览 148回答 2
2回答

心有法竹

function handleResponse(obj) {&nbsp; const container = document.getElementById("container")&nbsp; obj.items.forEach((rec, index) => {&nbsp; &nbsp; let singleBookCover =&nbsp; &nbsp; &nbsp; rec.volumeInfo.imageLinks && rec.volumeInfo.imageLinks.smallThumbnail;&nbsp; &nbsp; let singleBookTitle = rec.volumeInfo.title;&nbsp; &nbsp; let singleBookAuthor = rec.volumeInfo.authors[0];&nbsp; &nbsp; let book = document.createElement("div");&nbsp; &nbsp; book.className = "book"&nbsp; &nbsp; book.innerHTML = `&nbsp; &nbsp; <div><h1>${singleBookTitle}<h1>&nbsp; &nbsp; <p>${singleBookAuthor}</p>&nbsp; &nbsp; <img src="${singleBookCover}"></img>&nbsp; &nbsp; </div>&nbsp; &nbsp; `&nbsp; &nbsp; content.appendChild(book)&nbsp; });}&nbsp;<div id="content" class="books">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <script src="https://www.googleapis.com/books/v1/volumes?q=clean+code&callback=handleResponse"></script>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

料青山看我应如是

干净的答案 - 你应该使用document.appendChild(child)而不是 innerHTML 方法。此外,最近添加的 js 方法很少,可以帮助您操作大型 JSON 对象 - map、reduce、filter。我添加了示例,如何将原始对象清理为更小的数组,并将该数组中的项目插入 html-page。function demo (obj) {&nbsp; // getting all items from object&nbsp; const book = Object.keys(obj).map(item => obj['items']).reduce(&nbsp; &nbsp; (acc,rec, id, array) => {&nbsp; &nbsp; &nbsp; // getting Cover, Title, Author from each item&nbsp; &nbsp; &nbsp; let singleBookCover = rec[id].volumeInfo.imageLinks.thumbnail;&nbsp; &nbsp; &nbsp; let singleBookTitle = rec[id].volumeInfo.title;&nbsp; &nbsp; &nbsp; let singleBookAuthor = rec[id].volumeInfo.authors[0];&nbsp; &nbsp; &nbsp; // Creating new array only with Cover, Title, Author&nbsp; &nbsp; &nbsp; return [...acc, {singleBookCover, singleBookTitle, singleBookAuthor}]&nbsp; &nbsp; },&nbsp; &nbsp; []&nbsp; ).forEach( item => {&nbsp; &nbsp; // For each item on our array, we creating h1&nbsp; &nbsp; let title = document.createElement('h1');&nbsp; &nbsp; title.textContent = `${item.singleBookTitle} by ${item.singleBookAuthor}`;&nbsp; &nbsp; // img&nbsp; &nbsp; let img = document.createElement('img');&nbsp; &nbsp; img.src = item.singleBookCover;&nbsp; &nbsp; img.alt = `${item.singleBookTitle} by ${item.singleBookAuthor}`;&nbsp; &nbsp; // and div wrapper&nbsp; &nbsp; let container = document.createElement('div');&nbsp; &nbsp; // adding our child elements to wrapper&nbsp; &nbsp; container.appendChild(title).appendChild(img);&nbsp; &nbsp; // adding our wrapper to body&nbsp; &nbsp; document.body.appendChild(container);&nbsp; &nbsp;})&nbsp; return book}希望我的回答对你有帮助)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript