如何通过单击搜索结果来添加多张卡

发布于 2025-02-02 23:28:03 字数 1258 浏览 2 评论 0原文

我当前的脚本使您可以通过单击搜索栏的结果添加卡 +一些有关其内容的信息。

目的是创建一种电影列表。但是,我不知道如何,何处或是否应该实现一个循环来添加多张卡,而不是更换一张卡。

我最大的问题是,单击结果项目后,活动卡是 被新的代替。

第一个功能基于API加载内容。在单击研究中显示的项目后,它基本上可以清除搜索栏。它还称该功能负责将DIV(带有电影信息的卡)添加到HTML中。

function loadContentDetails(){
const searchListContent = searchList.querySelectorAll('.search-list-item');

searchListContent.forEach(content => {
    content.addEventListener('click', async () =>{
        searchList.classList.add('hide-search-list');
        searchBar.value = "";
        const result = await fetch(`http://www.omdbapi.com/?i=${content.dataset.id}&apikey=60ee3e91`);
        const contentDetails = await result.json();
        displayContentDetails(contentDetails);
    });
});}

第二个功能根据用户通过搜索栏选择的内容创建新卡。

function displayContentDetails(details){
cardsGroupFlex.innerHTML = `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;
}

感谢您的帮助。

My current script allows you to add a card + some info about its content by clicking on the result of a search bar.

The objective is to create a sort of movies list. However, I do not know how, where or if I should implement a loop to add multiple cards, instead of replacing one.

My biggest issue is that after clicking on the result item, the active card is
replaced by the new one.

The first function loads the content based on an API. It basically clears the search bar after clicking on the item shown through the research. It also calls the function responsible for adding a div (a card with the movie informations) to the HTML.

function loadContentDetails(){
const searchListContent = searchList.querySelectorAll('.search-list-item');

searchListContent.forEach(content => {
    content.addEventListener('click', async () =>{
        searchList.classList.add('hide-search-list');
        searchBar.value = "";
        const result = await fetch(`http://www.omdbapi.com/?i=${content.dataset.id}&apikey=60ee3e91`);
        const contentDetails = await result.json();
        displayContentDetails(contentDetails);
    });
});}

The second function creates a new card based on what the user selected through the search bar.

function displayContentDetails(details){
cardsGroupFlex.innerHTML = `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;
}

I would much appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小苏打饼 2025-02-09 23:28:03

一个简单的解决方案是,而不是设置innerhtmlcardsGroupflex,您可以将卡附加到innerhtml的末尾。

// instead of
cardsGroupFlex.innerHTML = `...`
// appending with +=
cardsGroupFlex.innerHTML += `...`

但是,如果您要实现删除或编辑卡之类的功能,则需要具有该卡详细信息的全局数组,并在displayContentDetails中循环循环。

该解决方案还可以利用附加到字符串的末端。

function displayContentDetails(){

  let newInner = ''

  for (const details of globalCards) {
    inner += `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `
  }
}

A simple solution is to instead of setting the innerHTML of cardsGroupFlex, you could append the card to the end of the innerHTML.

// instead of
cardsGroupFlex.innerHTML = `...`
// appending with +=
cardsGroupFlex.innerHTML += `...`

However if you're going to implement a feature like deleting or editing the cards, you're going to need to have a global array of the card's details, and looping over all of them inside displayContentDetails.

This solution could also make use of appending to the end of a string.

function displayContentDetails(){

  let newInner = ''

  for (const details of globalCards) {
    inner += `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `
  }
}
那请放手 2025-02-09 23:28:03

更改cardsGroupflex.innerhtml = cardsgroupflex.innerhtml += (在“ =”之前添加一个“ +”符号)。这将 condenate in innerhtml的新卡,而不是用新卡替换InnerHTML:

cardsGroupFlex.innerHTML += `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;

Change cardsGroupFlex.innerHTML = to cardsGroupFlex.innerHTML += (add a "+" sign before the "="). This will concatenate a new card to the innerHTML rather than replacing the innerHTML with the new card:

cardsGroupFlex.innerHTML += `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;
少年亿悲伤 2025-02-09 23:28:03

一个快速注意:不要将searchlist.classlist.add用作函数/方法。这是一个属性。因此,请遵循添加类。

searchList.classList.add = 'hide-search-list';

这只是对您的代码的快速观察。

One quick note: don't use searchList.classList.add as a function/method. It's a property. So do as following to add classes.

searchList.classList.add = 'hide-search-list';

This is just from a quick observation on your code.

甜`诱少女 2025-02-09 23:28:03

您是覆盖innerhtml而不是附加到它的。您可以附加它,请参阅摘要1

let cardsGroupFlex = document.getElementById("cardsGroupFlex");
function displayContentDetails(details){
cardsGroupFlex.innerHTML += `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;
}
let detailArray = [
    {
        Poster: "Poster1",
        Title: "Title1",
        Plot: "Plot1"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
];

for (let d of detailArray) displayContentDetails(d);
<div id="cardsGroupFlex"></div>

但是,从性能的角度来看,始终将InnerHTML添加到InnerHtml并不是很健康,因此,让我们首先通过生成结构来改进上面的代码,然后才设置innerhtml

let cardsGroupFlex = document.getElementById("cardsGroupFlex");
function getContentDetails(details){
return `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;
}
let detailArray = [
    {
        Poster: "Poster1",
        Title: "Title1",
        Plot: "Plot1"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
];

cardsGroupFlex.innerHTML = detailArray.map(item => getContentDetails(item)).join("");
<div id="cardsGroupFlex"></div>

You are overriding the innerHTML instead of appending to it. You can append it, see snippet 1

let cardsGroupFlex = document.getElementById("cardsGroupFlex");
function displayContentDetails(details){
cardsGroupFlex.innerHTML += `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;
}
let detailArray = [
    {
        Poster: "Poster1",
        Title: "Title1",
        Plot: "Plot1"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
];

for (let d of detailArray) displayContentDetails(d);
<div id="cardsGroupFlex"></div>

Yet, it is not very healthy to always append to innerHTML from the perspective of performance, so let's improve the code above by generating the structure first and only then set the innerHTML:

let cardsGroupFlex = document.getElementById("cardsGroupFlex");
function getContentDetails(details){
return `
    <div class="card">
        <img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
        <div class="card-description">
            <p class="card-title">${details.Title}</p>
            <p> ${(details.Plot)} </p>
        </div>
    </div>
    `;
}
let detailArray = [
    {
        Poster: "Poster1",
        Title: "Title1",
        Plot: "Plot1"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
    {
        Poster: "Poster2",
        Title: "Title2",
        Plot: "Plot2"
    },
];

cardsGroupFlex.innerHTML = detailArray.map(item => getContentDetails(item)).join("");
<div id="cardsGroupFlex"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文