<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <script type="text/tempalte" >
        我叫<% name %>,我<% age %>岁,我在 <% school %>上学
    </script>
    <script type="text/javascript">
        //创建对象
        var obj = {
            "name": "张三",
            "age": 18,
            "school": "蓝翔技校"
        }
        //  把上面的obj中的数据绑定到tmpl中,输出完整的一句话
        //  正则 认识<% value %>格式
        var reg = /<%\s*([^%>]\S+)\s*%>/;

        function tempalte(id, obj) {
            var html = document.getElementById(id).innerHTML;
			
            var _exec = null;//初始化
            while (_exec = (reg.exec(html))) {
                html = html.replace(_exec[0], obj[_exec[1]]);
            }
            return html;
        }
       document.write(tempalte('tmpl', obj))
    </script>

</body>
</html>

相关文章:

  • 2021-12-19
  • 2021-11-04
  • 2022-12-23
  • 2022-01-28
  • 2021-11-07
  • 2021-09-21
  • 2021-10-19
  • 2021-09-18
猜你喜欢
  • 2022-12-23
  • 2021-12-21
  • 2021-12-02
  • 2021-12-03
  • 2021-08-30
  • 2021-12-28
  • 2022-02-08
相关资源
相似解决方案