jQuery 动态数据获取与传参示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery 动态数据获取与传参示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 10px 0;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #fff;
            min-height: 50px;
        }
        .user-card {
            border: 1px solid #ccc;
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
            background-color: white;
        }
    </style>
</head>
<body>
    <h1>jQuery 动态数据获取与传参示例</h1>
    
    <div class="container">
        <h2>示例1: 获取用户数据</h2>
        <button id="getUserBtn">获取用户数据</button>
        <div id="userResult"></div>
    </div>

    <div class="container">
        <h2>示例2: 带参数请求</h2>
        <div>
            <label for="userId">输入用户ID:</label>
            <input type="number" id="userId" min="1" max="10" value="1">
            <button id="getUserByIdBtn">获取指定用户</button>
        </div>
        <div id="userByIdResult"></div>
    </div>

    <div class="container">
        <h2>示例3: 动态生成内容</h2>
        <button id="generatePostsBtn">生成帖子列表</button>
        <div id="postsContainer"></div>
    </div>

    <!-- 引入 jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <script>
        $(document).ready(function() {
            // 示例1: 获取用户数据
            $("#getUserBtn").click(function() {
                $.ajax({
                    url: "https://jsonplaceholder.typicode.com/users/1",
                    method: "GET",
                    dataType: "json",
                    beforeSend: function() {
                        $("#userResult").html("加载中...");
                    },
                    success: function(data) {
                        let html = `
                            <div class="user-card">
                                <h3>${data.name}</h3>
                                <p>邮箱: ${data.email}</p>
                                <p>电话: ${data.phone}</p>
                                <p>公司: ${data.company.name}</p>
                            </div>
                        `;
                        $("#userResult").html(html);
                    },
                    error: function() {
                        $("#userResult").html("加载数据失败");
                    }
                });
            });

            // 示例2: 带参数请求
            $("#getUserByIdBtn").click(function() {
                const userId = $("#userId").val();
                if (!userId) {
                    alert("请输入用户ID");
                    return;
                }

                $.ajax({
                    url: `https://jsonplaceholder.typicode.com/users/${userId}`,
                    method: "GET",
                    dataType: "json",
                    beforeSend: function() {
                        $("#userByIdResult").html("加载中...");
                    },
                    success: function(data) {
                        let html = `
                            <div class="user-card">
                                <h3>${data.name}</h3>
                                <p>用户名: ${data.username}</p>
                                <p>邮箱: ${data.email}</p>
                                <p>地址: ${data.address.street}, ${data.address.city}</p>
                            </div>
                        `;
                        $("#userByIdResult").html(html);
                    },
                    error: function() {
                        $("#userByIdResult").html("找不到该用户");
                    }
                });
            });

            // 示例3: 动态生成内容
            $("#generatePostsBtn").click(function() {
                $.ajax({
                    url: "https://jsonplaceholder.typicode.com/posts",
                    method: "GET",
                    dataType: "json",
                    beforeSend: function() {
                        $("#postsContainer").html("加载中...");
                    },
                    success: function(data) {
                        let html = "<h3>最新帖子:</h3>";
                        // 只显示前5条
                        data.slice(0, 5).forEach(post => {
                            html += `
                                <div class="user-card">
                                    <h4>${post.title}</h4>
                                    <p>${post.body.substring(0, 100)}...</p>
                                    <button class="view-post-btn" data-post-id="${post.id}">查看详情</button>
                                </div>
                            `;
                        });
                        $("#postsContainer").html(html);
                        
                        // 为动态生成的按钮添加事件
                        $(".view-post-btn").click(function() {
                            const postId = $(this).data("post-id");
                            alert(`你点击了帖子ID: ${postId}\n这里可以跳转到详情页或显示更多内容。`);
                        });
                    },
                    error: function() {
                        $("#postsContainer").html("加载帖子失败");
                    }
                });
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值