template模版与Underscore.js

时间:2023-03-09 15:31:12
template模版与Underscore.js

template模版与Underscore.js

在项目中经常使用的模版是Underscore这个js框架的实用功能。

在html里面设定模板,然后js绑定数据,这样能避免在js中出现非常多的html标签,在项目中表格标签用的比较多,用于追加数据,静态表格tr条数的增加。

例如:定义一个表格行模版

template模版与Underscore.js

  通过js绑定数据,追加到表格尾,实现静态增加数据。

1:获取渲染元素和模板内容:$("#trtemplate").html()

2:解析模板, 返回解析后的内容:n = _.template($("#trtemplate").html()); n(mod.toJSON())-->生成新数据行,可追加到行尾或覆盖。

template模版与Underscore.js