jQuery はバックエンドからデータを取得してページにレンダリングする機能を実装します。

多くは言う必要はありません。まずは効果から始めましょう

ボタンをクリックしてページのレンダリングを実行します

インターフェイスのドキュメント

注: インターフェイス ドキュメントとバックエンド アドレスは [Dark Horse Programmer WeChat Mini Program Development Front-end Tutorial_Zero-Basic Fun with WeChat Mini Program] https://www.bilibili.com/video/BV1nE41117BQ/?p=57&share_sourceからのものです。 =copy_web&vd_source= 81202236c582d64e6a1e57dd79b18f0f Bステーションビデオコース

コードを実装する

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/jquery.js"></script>
    <style>
        button{
            width: 200px;
            height: 100px;
            background-color: #59de8c;
        }
        .box{
            float: left;
            margin-left: 50px;
            padding-top: 30px;
            width: 200px;
            height: 250px;
            background-color: #df75c1;
            text-align: center;
            color: aquamarine;
            text-shadow: 2px 2px 2px red;
        }
    </style>
</head>
<body>
    <button>点击后用jQuery渲染页面</button>
        <script>
            $('button').click(function(){
                // 按钮点击后隐藏
                $(this).hide()
                // 发送get请求
                $.ajax({
                type:'get',
                url:'https://api-hmugo-web.itheima.net/api/public/v1/home/catitems',
                success:function(data){  
                    //将获取到的数据渲染到页面上
                   for(let i = 0; i < data.message.length;i++){
                    $("body").append(`<div class="box">
                            <img src=${data.message[i].image_src} alt="">
                            <h2>标题:${data.message[i].name}</h2>
                     </div>`)
                   }
                }
            })
            })
        </script>
</body>
</html>

要約:

  1. jQuery を使用してhttps://api-hmugo-web.itheima.net/api/public/v1/home/catitemsに取得リクエストを送信し、データを取得します

  1. データが正常に取得されると、データは $("body").append( ) を通じてページにレンダリングされます (ここでもテンプレート文字列が使用されます)。

おすすめ

転載: blog.csdn.net/weixin_53141315/article/details/129459056