underscore模板引擎

学习网站http://www.css88.com

<!-- TODO -->
<!--使用了undersocre的each方法
第一个参数  就当前遍历的对象
第二个从那时当前遍历的索引
-->
<script type="text/template" id="template">
    <div>
        <%_.each(model,function(item,i){%>
            <p><%-item.name%></p>
        <%});%>
    </div>
</script>
<script src="../lib/underscore/underscore-min.js"></script>
<script>
    $(function(){
        /*
        * - 是直接渲染成字符
        * = 直接填充在html  标签是会被渲染
        * XSS攻击:跨站脚本攻击(Cross Site Scripting),
        * 为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆
        * 故将跨站脚本攻击缩写为XSS。
        * */
        /* 准备 json  数据*/
        var data = [{name:'xiaoming'},{name:'xiaohong'}];
        /* 参数是模板当中的字符串  使用template方法 返回一个模板对象*/
        var template = _.template($('#template').html());
        /*调用模板对象的时候  传入json数据  返回的就是解析过后的字符串*/
        $('#box').html( template({model:data}));
    });
</script>

 

转载于:https://my.oschina.net/u/3395167/blog/903268

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值