zoukankan      html  css  js  c++  java
  • 我是这样使用template.js来异步渲染数据的demo

    直接来代码吧!

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <ul id="wrap">
                <script id="tpl" type="text/html">
                    <%if(result){%>
                        <%for(var i=0;i<result.length;i++){%>
                            <li><%=result[i].area_name%></li>
                        <%}%>
                    <%}%>
                </script>
            </ul>
            <script src="js/jquery/jquery-1.8.3.min.js" ></script>
            <script src="js/template/template.js"></script>
            <script>
                $(function(){
                
                    $.ajax({
                         type: "get", 
                         url: "js/json/package.json",       
                         data:{//后台需要的参数
                             flag:"wap"
                         }, 
                         dataType: "json",
                         success: function(data){
    //                        var data = {
    //                            result: [
    //                                {
    //                                    area_id: "535",
    //                                    area_name: "美国",
    //                                    pid: "45055"
    //                                },
    //                                {
    //                                    area_id: "536",
    //                                    area_name: "加拿大",
    //                                    pid: "45055"
    //                                }
    //                            ],
    //                            error: "0",
    //                            msg: "",
    //                            total: 0,
    //                            hasmore: false
    //                        }
                             
                            if (data && data.error === "0") {
                                console.log(data);
                                var html = template.render('tpl',data);
                                $('#wrap').html(html);
                            }
                        }
                    });
                });
            </script>
        </body>
    </html>
    package.json:
    {
        "result": [
            {
                "area_id": "535",
                "area_name": "美国",
                "pid": "45055"
            },
            {
                "area_id": "536",
                "area_name": "加拿大",
                "pid": "45055"
            }
        ],
        "error": "0",
        "msg": "",
        "total": 0,
        "hasmore": false
    }

    需要的js文件就是 template.js和jQuery

    (template.js查看文件)!!!!

  • 相关阅读:
    搜索复习-中级水题
    搜索复习-基础水题(一共12道)
    TCPThree_C杯 Day1
    bzoj1579 道路升级
    bzoj3732 Network(NOIP2013 货车运输)
    bzoj1624 寻宝之路
    bzoj1430 小猴打架
    bzoj2763 飞行路线
    2017-10-28-afternoon-清北模拟赛
    2017-10-28-morning-清北模拟赛
  • 原文地址:https://www.cnblogs.com/xiangru0921/p/6689556.html
Copyright © 2011-2022 走看看