dom基础3 — 简易版template.js
<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>