javascript利用初始化數(shù)據(jù)裝配模版的實(shí)現(xiàn)代碼
更新時(shí)間:2010年11月17日 20:28:03 作者:
實(shí)現(xiàn)一個(gè)通用方法,使用初始化數(shù)據(jù)來裝配模版。需要的朋友可以參考下。
var list = [{id:1, name:"czone", age:21}, {id:2, name:'czonechan', age:21}];
var template ='<div id="p$id"><span class="name">$name</span><span class="age">$age</span></div>';
實(shí)現(xiàn)一個(gè)通用方法,使用初始化數(shù)據(jù)來裝配模版。
function displayTemplate(list,template){
var result="";
for(var i=0,l=list.length;i<l;i++){
var temp=template;
for(var k in list[i])
{
var reg=new RegExp("\\$"+k,"g");
temp=temp.replace(reg,function(w){
return list[i][k];
})
}
result+=temp;
}
return result;
}
var template ='<div id="p$id"><span class="name">$name</span><span class="age">$age</span></div>';
實(shí)現(xiàn)一個(gè)通用方法,使用初始化數(shù)據(jù)來裝配模版。
復(fù)制代碼 代碼如下:
function displayTemplate(list,template){
var result="";
for(var i=0,l=list.length;i<l;i++){
var temp=template;
for(var k in list[i])
{
var reg=new RegExp("\\$"+k,"g");
temp=temp.replace(reg,function(w){
return list[i][k];
})
}
result+=temp;
}
return result;
}
相關(guān)文章
javascript實(shí)現(xiàn)數(shù)字+字母驗(yàn)證碼的簡單實(shí)例
本篇文章只要是對javascript實(shí)現(xiàn)數(shù)字+字母驗(yàn)證碼的簡單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02JS實(shí)現(xiàn)超簡單的鼠標(biāo)拖動效果
這篇文章主要介紹了JS實(shí)現(xiàn)超簡單的鼠標(biāo)拖動效果,涉及JavaScript響應(yīng)鼠標(biāo)事件動態(tài)操作頁面元素的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11javascript實(shí)現(xiàn)顯示和隱藏div方法匯總
本文章通過幾個(gè)簡單的實(shí)例告訴你如何來實(shí)例關(guān)于隱藏與顯示div層及關(guān)閉層與隱藏的區(qū)別分析哦,有需要的同學(xué)可以參考一下本文章。2015-08-08