0

0

使用 Pokémon API 创建新页面显示更多信息

霞舞

霞舞

发布时间:2025-08-05 16:18:01

|

346人浏览过

|

来源于php中文网

原创

使用 pokémon api 创建新页面显示更多信息

本文档将指导您如何使用 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 代码:

<div id="details">
    <h2>Pokemon Details</h2>
    <div class="more-details"></div>
</div>
<script>
const moreDetails = document.querySelector(".more-details");
function detailsPokemon(poke) {
    moreDetails.innerHTML = `
        <div class="card-pokemon">
            <span>ID: ${poke.id}</span>
            <h2 class="pokemon-nombre">${poke.name}</h2>
        </div>
    `;
}
const endPoint = "https://pokeapi.co/api/v2/pokemon/";
fetch(endPoint + new URLSearchParams(location.search).get('id'))
    .then((response) => response.json())
    .then(data => detailsPokemon(data))
</script>

这段代码首先获取 more-details 元素的引用,该元素将用于显示 Pokémon 详细信息。然后,定义了一个 detailsPokemon 函数,该函数接收一个 Pokémon 对象并将其详细信息添加到 more-details 元素中。

Fotor
Fotor

Fotor 在线照片编辑器

下载

最重要的是,代码使用 new URLSearchParams(location.search).get('id') 从 URL 查询参数中获取 Pokémon ID。然后,使用该 ID 从 Pokémon API 获取 Pokémon 详细信息,并调用 detailsPokemon 函数来显示这些信息。

完整示例

以下是完整的 index.html 和 details.html 文件:

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Pokemon List</title>
</head>
<body>
    <main>
        <div>
            <button id="btn-pokemon" type="button">Click to see Pokemon</button>
        </div>

        <div class="card" id="details" >
            <div class="more-details"></div>
        </div>

        <div class="container-pokemon" id="cardPokemon"></div>

    </main>

    <script>
        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) => `<p>${type.type.name}</p>`);
            tipoDePokemon = tipoDePokemon.join('');

            let abilitiesPokemon = poke.abilities.map((ability) => `<p>${ability.ability.name}</p>`);
            abilitiesPokemon = abilitiesPokemon.join('');

           const containerPokemon = document.createElement("div");

           containerPokemon.innerHTML = `
                <div class="card-pokemon">
                    <span>${poke.id}</span>
                    <div class="img">
                        <img src="${poke.sprites.other["official-artwork"].front_default}" alt="${poke.name}">
                    </div>
                    <div class="info">
                        <div>
                            <h2>${poke.name}</h2>
                            <div>
                                <p>Tipo de Pokemon:</>
                                ${tipoDePokemon}
                                <p>base_experience:</>
                                ${poke.base_experience}
                            </div>
                            <div>
                                <p class="stat">${poke.height}m</p>
                                <p class="stat">${poke.weight}kg</p>
                            </div>
                    </div>
                    <button class="btn-detalle" onclick="seeDetail(${poke.id})"> MORE INFO </button> 

                </div>
            `;
            cardPokemon.append(containerPokemon)
        }

        function seeDetail(id){
            window.open('details.html?id=' + id, '_blank');
        }


        btnPokemon.addEventListener('click', function() {

            for (let i = 1; i <= 100; i++) {
                fetch(endPoint + i)
                .then((response) => response.json())
                .then(data => showPokemon(data))
            }

        })
    </script>
</body>
</html>

details.html

<!DOCTYPE html>
<html>
<head>
    <title>Pokemon Details</title>
</head>
<body>
    <div id="details">
        <h2>Pokemon Details</h2>
        <div class="more-details"></div>
    </div>
    <script>
        const moreDetails = document.querySelector(".more-details");
        function detailsPokemon(poke) {
            moreDetails.innerHTML = `
                <div class="card-pokemon">
                    <span>ID: ${poke.id}</span>
                    <h2 class="pokemon-nombre">${poke.name}</h2>
                </div>
            `;
        }
        const endPoint = "https://pokeapi.co/api/v2/pokemon/";
        fetch(endPoint + new URLSearchParams(location.search).get('id'))
            .then((response) => response.json())
            .then(data => detailsPokemon(data))
    </script>
</body>
</html>

总结

通过使用 URL 查询参数,我们能够轻松地将 Pokémon ID 从 index.html 传递到 details.html,并在新页面中显示特定 Pokémon 的详细信息。这种方法简单有效,并且易于理解和实现。 记住要确保你的 details.html 文件能够正确地从 URL 中提取 ID 并使用它来获取数据。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
js正则表达式
js正则表达式

php中文网为大家提供各种js正则表达式语法大全以及各种js正则表达式使用的方法,还有更多js正则表达式的相关文章、相关下载、相关课程,供大家免费下载体验。

531

2023.06.20

js获取当前时间
js获取当前时间

JS全称JavaScript,是一种具有函数优先的轻量级,解释型或即时编译型的编程语言;它是一种属于网络的高级脚本语言,主要用于Web,常用来为网页添加各式各样的动态功能。js怎么获取当前时间呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

576

2023.07.28

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

761

2023.08.03

js是什么意思
js是什么意思

JS是JavaScript的缩写,它是一种广泛应用于网页开发的脚本语言。JavaScript是一种解释性的、基于对象和事件驱动的编程语言,通常用于为网页增加交互性和动态性。它可以在网页上实现复杂的功能和效果,如表单验证、页面元素操作、动画效果、数据交互等。

6309

2023.08.17

js删除节点的方法
js删除节点的方法

js删除节点的方法有:1、removeChild()方法,用于从父节点中移除指定的子节点,它需要两个参数,第一个参数是要删除的子节点,第二个参数是父节点;2、parentNode.removeChild()方法,可以直接通过父节点调用来删除子节点;3、remove()方法,可以直接删除节点,而无需指定父节点;4、innerHTML属性,用于删除节点的内容。

494

2023.09.01

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

221

2023.09.04

Js中concat和push的区别
Js中concat和push的区别

Js中concat和push的区别:1、concat用于将两个或多个数组合并成一个新数组,并返回这个新数组,而push用于向数组的末尾添加一个或多个元素,并返回修改后的数组的新长度;2、concat不会修改原始数组,是创建新的数组,而push会修改原数组,将新元素添加到原数组的末尾等等。本专题为大家提供concat和push相关的文章、下载、课程内容,供大家免费下载体验。

240

2023.09.14

js截取字符串的方法介绍
js截取字符串的方法介绍

JavaScript字符串截取方法,包括substring、slice、substr、charAt和split方法。这些方法可以根据具体需求,灵活地截取字符串的不同部分。在实际开发中,根据具体情况选择合适的方法进行字符串截取,能够提高代码的效率和可读性 。

303

2023.09.21

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

69

2026.03.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
10分钟--Midjourney创作自己的漫画
10分钟--Midjourney创作自己的漫画

共1课时 | 0.1万人学习

Midjourney 关键词系列整合
Midjourney 关键词系列整合

共13课时 | 1.0万人学习

AI绘画教程
AI绘画教程

共2课时 | 0.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号