JS使用正则+JSON对HTML模板进行数据填充
2017-06-15 19:37
519 查看
<style>
ul, li {
list-style: none;
}
.container {
width: 200px;
margin: 20px auto;
padding: 5px;
background-color: #1B6540;
}
.list_box li {
color: #fff;
height: 60px;
margin-bottom: 5px;
background-color: #4FA46B;
}
</style>
<div class="container">
<ul class="list_box" id="list_box">
<script type="text/template" data-number="list_tple">
<li class="list_item">
{# $title$ 之间的字符串表示json的key #}
<p>$content$</p>
<h3>$title$</h3>
</li>
</script>
</ul>
</div>
<script>
json_data = {
page: "1",
records: "3",
data: [{
title: "标题1",
content: "内容1"
}, {
title: "标题2",
content: "内容2"
}, {
title: "标题3",
content: "内容3"
}, {
title: "标题4",
content: "内容4"
}, {
title: "标题5",
content: "内容5"
},]
}
String.prototype.tmple = function (obj) {
// '/' 一般用于 正则的开始和结束,中途要使用/就得用\符号来转义了,
// '\$' 我猜想是值 $$ 之间的东西
// '\w+' 匹配任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。
// '/g' 意思就是:global可选标志,带这个标志表示替换将针对行中每个匹配的串进行,否则则只替换行中第一个匹配串
return this.replace(/\$\w+\$/g, function (matchs) {
var returns = obj[matchs.replace(/\$/g, "")];
return (returns + "") == "undefined" ? "" : returns;
});
};
//使用 data-* 属性来嵌入自定义数据: 这个用的是id
var htmlList = '',
htmlTemp = $("#list_box script[data-number='list_tple']").html();
json_data.data.forEach(function (object) {
// tmple 这里指正则表达式
htmlList += htmlTemp.tmple(object);
});
$("#list_box").html(htmlList);
</script>
ul, li {
list-style: none;
}
.container {
width: 200px;
margin: 20px auto;
padding: 5px;
background-color: #1B6540;
}
.list_box li {
color: #fff;
height: 60px;
margin-bottom: 5px;
background-color: #4FA46B;
}
</style>
<div class="container">
<ul class="list_box" id="list_box">
<script type="text/template" data-number="list_tple">
<li class="list_item">
{# $title$ 之间的字符串表示json的key #}
<p>$content$</p>
<h3>$title$</h3>
</li>
</script>
</ul>
</div>
<script>
json_data = {
page: "1",
records: "3",
data: [{
title: "标题1",
content: "内容1"
}, {
title: "标题2",
content: "内容2"
}, {
title: "标题3",
content: "内容3"
}, {
title: "标题4",
content: "内容4"
}, {
title: "标题5",
content: "内容5"
},]
}
String.prototype.tmple = function (obj) {
// '/' 一般用于 正则的开始和结束,中途要使用/就得用\符号来转义了,
// '\$' 我猜想是值 $$ 之间的东西
// '\w+' 匹配任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。
// '/g' 意思就是:global可选标志,带这个标志表示替换将针对行中每个匹配的串进行,否则则只替换行中第一个匹配串
return this.replace(/\$\w+\$/g, function (matchs) {
var returns = obj[matchs.replace(/\$/g, "")];
return (returns + "") == "undefined" ? "" : returns;
});
};
//使用 data-* 属性来嵌入自定义数据: 这个用的是id
var htmlList = '',
htmlTemp = $("#list_box script[data-number='list_tple']").html();
json_data.data.forEach(function (object) {
// tmple 这里指正则表达式
htmlList += htmlTemp.tmple(object);
});
$("#list_box").html(htmlList);
</script>
相关文章推荐
- JS使用正则+JSON对HTML模板进行数据填充
- JS使用模板快速填充HTML控件数据
- JS使用模板快速填充HTML控件数据 --- 自己写组件(0)
- JS使用模板快速填充HTML控件数据
- JS使用模板快速填充HTML控件数据
- js使用模板快速填充数据
- 利用tempo将json数据填充到html模板
- js与php之间使用json格式进行数据交互
- Jquery 模板插件 jquery.tmpl.js 的使用方法(1):基本语法,绑定,each循环,ajax获取json数据
- 使用Java将PDF解析成HTML页面进行展示并从页面中提取Json数据设置到Table中
- 使用正则进行客户端向service捕捉html数据
- eval解析JSON中的注意点 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数。 2. 使用Function对象来进行返回解析。 使用eval
- MVC客户端使用 Mustache.js把json数据填充到模版中
- 在html页面上使用ajax传递json数据到基于express框架(node.js)的服务器
- 使用ajax+HTML+servlet实现数据模板填充
- 使用正则表达式进行xml数据验证
- 掌握 Ajax,第 10 部分: 使用 JSON 进行数据传输
- 掌握 Ajax,第 10 部分: 使用 JSON 进行数据传输
- 使用javascript和正则表达式进行数据验证
- 使用正则表达式进行xml数据验证