Home  >  Article  >  Web Front-end  >  Custom js template loading data file example sharing

Custom js template loading data file example sharing

小云云
小云云Original
2018-03-28 17:21:231397browse

本文主要和大家分享自定义js模板加载数据文件实例,主要以代码形式和大家分享,希望能帮助到大家。

1、代码如下:

var model={
		reg:/\$\{([\w]*)\}/ig,
		/** 填充列表模板 */
		loaderList:function(list, template, container){
			for(var i=0; i<list.length; i++){
				var obj = list[i];
				this.loaderInfo(obj, template, container);
			}

		},
		/** 填充表单模板 */
		loaderInfo:function(obj, template, container){
			var html = template.replace(model.reg, function(s,value) {
			   return eval("obj."+value);
			});
			$(container).append(html);
		}
};

2、使用

// 变量初始化
	var host = window.location.href;
	host = host.substring(0, host.indexOf(&#39;before&#39;, 0));
		
	$(function() {
		var param =getRequest();
		var url = host + "api/article/"+param.id+"/info.ht";
		var template = $("#detail_info").html();
		$.get(url, function(result) {
			var obj = result.data;
			model.loaderInfo(obj, template, "#container");

		});
	});

备注:

正则表达式中/i,/g,/ig,/gi,/m的区别和含义

/i (忽略大小写)
/g (全文查找出现的所有匹配字符)
/m (多行查找)
/gi(全文查找、忽略大小写)
/ig(全文查找、忽略大小写)

相关推荐:

js实现滚动鼠标加载数据

The above is the detailed content of Custom js template loading data file example sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn