template-native.js帮后台请求过来的数据渲染在页面上

19 篇文章 0 订阅

template-native.js帮后台请求过来的数据渲染在页面上

话不多说,直接上代码。感觉好笨重 ,用惯了vue,再来用这个感觉天塌下来了

自己网上下载一个 template-native.js 其他代码直接复制到编辑器即可运行

<html>

    <head>
        <title>template-native模板渲染</title>
        <meta charset="UTF-8" />
        <script src="js/template-native.js" type="text/javascript" charset="utf-8"></script>
        <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }

        #area ul li {
            width: 100%;
            height: 75px;
            background: #1715151a;
            padding: 10px 10px;
            margin: 5px;
            position: relative;
            overflow: hidden;
        }

        #area ul li .imgs {
            width: 72px;
            height: 72px;
            position: absolute;
            top: 3px;
            left: 5px;
            border-radius: 100%;
        }

        #area ul li .imgs img {
            width: 100%;
            height: 100%;
            border-radius: 100%;
        }

        #area ul li .text {
            width: 100%;
            height: 20px;
            /* background: darkcyan; */
            position: absolute;
            top: 16px;
            left: 87px }

        #area ul li .info {
            width: 100%;
            height: 20px;
            /* background: darkcyan; */
            position: absolute;
            top: 50px;
            left: 87px }
    </style>

    <body>
        <div id="area"></div>

        <!--用于存放模版-->
        <script id="template" type="text/html">
            <div>
                <ul>

                    <% for(i=0;i<list.length;++i) {%>
                    <li>
                        <div class="imgs">
                            <img src=<%=list[i].profile_image%> alt="" />
                        </div>
                        <div class="text">
                            标题:
                            <%=list[i].name%>
                        </div>
                        <div class="info">
                            内容:
                            <%=list[i].text%>
                        </div>
                        <%}%>

                    </li>

                </ul>
            </div>
        </script>
        <!--用于渲染模版-->
        <script>
            $.ajax({
                url: 'https://www.apiopen.top/satinApi?type=1&page=1',
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function(data) {
                    //免费接口拿到的数据
                    console.log(JSON.stringify(data));
                    //帮拿到的数据存在一个变量中             
                    var arr = {
                        list: data.data//至于这里为什么是data.data自己访问一下这个接口看看数据格式即可
                    }
                    var html = template('template', arr);//arr就是上两行代码创建的
                    document.getElementById('area').innerHTML = html//渲染到哪个dom中
                }
            });
        </script>
    </body>

</html>

例子展示
这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1登峰造极

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值