
本文档将指导您如何使用 Pokémon API,在点击“更多信息”按钮后,将特定 Pokémon 的详细信息展示在一个新的 HTML 页面中。我们将利用 URL 查询参数传递 Pokémon 的 ID,并在新页面中获取并显示这些信息。
通过 URL 查询参数传递 Pokémon ID
为了在新页面中显示特定 Pokémon 的详细信息,我们需要一种方法来告诉 details.html 页面要显示哪个 Pokémon。最常用的方法之一是使用 URL 查询参数。
在 index.html 的 App.js 文件中,修改 seeDetail 函数,将 Pokémon 的 ID 作为查询参数添加到 details.html 的 URL 中:
function seeDetail(id){ window.open('details.html?id=' + id, '_blank');}
现在,当点击“更多信息”按钮时,details.html 页面将在新的标签页中打开,并且 URL 将包含 Pokémon 的 ID,例如:details.html?id=25。
在 details.html 中获取并显示 Pokémon 详细信息
接下来,我们需要在 details.html 页面中获取 URL 查询参数中的 Pokémon ID,并使用该 ID 从 Pokémon API 获取详细信息。
在 details.html 页面中,添加以下 JavaScript 代码:
const moreDetails = document.querySelector(".more-details");function detailsPokemon(poke) { moreDetails.innerHTML = `Pokemon Details
ID: ${poke.id}`;}const endPoint = "https://pokeapi.co/api/v2/pokemon/";fetch(endPoint + new URLSearchParams(location.search).get('id')) .then((response) => response.json()) .then(data => detailsPokemon(data))${poke.name}
这段代码首先获取 more-details 元素的引用,该元素将用于显示 Pokémon 详细信息。然后,定义了一个 detailsPokemon 函数,该函数接收一个 Pokémon 对象并将其详细信息添加到 more-details 元素中。
最重要的是,代码使用 new URLSearchParams(location.search).get(‘id’) 从 URL 查询参数中获取 Pokémon ID。然后,使用该 ID 从 Pokémon API 获取 Pokémon 详细信息,并调用 detailsPokemon 函数来显示这些信息。
完整示例
以下是完整的 index.html 和 details.html 文件:
index.html
Pokemon List const cardPokemon = document.querySelector("#cardPokemon"); const cardDetalle = document.querySelector('#detalle'); const details = document.querySelector('.more-details'); const btnPokemon = document.querySelector('#btn-pokemon'); const endPoint = "https://pokeapi.co/api/v2/pokemon/"; function showPokemon(poke) { let tipoDePokemon = poke.types.map((type) => ` ${type.type.name}
`); tipoDePokemon = tipoDePokemon.join(''); let abilitiesPokemon = poke.abilities.map((ability) => `${ability.ability.name}
`); abilitiesPokemon = abilitiesPokemon.join(''); const containerPokemon = document.createElement("div"); containerPokemon.innerHTML = `${poke.id}@@##@@`; cardPokemon.append(containerPokemon) } function seeDetail(id){ window.open('details.html?id=' + id, '_blank'); } btnPokemon.addEventListener('click', function() { for (let i = 1; i response.json()) .then(data => showPokemon(data)) } })${poke.name}
Tipo de Pokemon:> ${tipoDePokemon} <p>base_experience:> ${poke.base_experience} </div>
${poke.height}m
${poke.weight}kg
details.html
Pokemon Details const moreDetails = document.querySelector(".more-details"); function detailsPokemon(poke) { moreDetails.innerHTML = `Pokemon Details
ID: ${poke.id}`; } const endPoint = "https://pokeapi.co/api/v2/pokemon/"; fetch(endPoint + new URLSearchParams(location.search).get('id')) .then((response) => response.json()) .then(data => detailsPokemon(data))${poke.name}
总结
通过使用 URL 查询参数,我们能够轻松地将 Pokémon ID 从 index.html 传递到 details.html,并在新页面中显示特定 Pokémon 的详细信息。这种方法简单有效,并且易于理解和实现。 记住要确保你的 details.html 文件能够正确地从 URL 中提取 ID 并使用它来获取数据。
以上就是使用 Pokémon API 创建新页面显示更多信息的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1513757.html赞 (0)打赏微信扫一扫
支付宝扫一扫
微信扫一扫
支付宝扫一扫