首页 > Web开发 > 详细

dom基础3 — 简易版template.js

时间:2019-03-24 20:08:58      阅读:159      评论:0      收藏:0      [点我收藏+]
<template id="template">
    <div class="content" data-id="{{id}}">
        {{content}}
    </div>
</template>

<script type="text/javascript">
    "use strict";
    var console = window.console; 


    var htmlTpl = document.getElementById(template).innerHTML;
    var data = {
        id: 1,
        content: 我是一段文字
    }

    function template(htmlTpl,data) {
        return htmlTpl.replace(/{{\w+}}/g,function(matchs){
            matchs = matchs.replace(/{{/g,‘‘).replace(/}}/g,‘‘);
            matchs = data[matchs];
            return matchs;
        });

    }
    console.log(template(htmlTpl,data));
</script>

 

dom基础3 — 简易版template.js

原文:https://www.cnblogs.com/popeyesailorman/p/10589486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!