模板引擎 laytpl 二次刷新问题

提问 已结 4 580
Elylon
Elylon VIP3 2019-3-12
悬赏:20飞吻
版本:layui 2.4.5 浏览器:Chrome最新版
模板引擎在进行第二次刷新的时候会找不到<script id="userInfo" type="text/html"></script>

<div class="layui-row" id="userInfoView">
<script id="userInfo" type="text/html">
<div class="layui-col-md3">
<div class="item">
<span class="key">id</span>
<span class="value">{{d.id}}</span>
</div>
<div class="item">
<span class="key">昵称</span>
<span class="value">{{d.name}}</span>
</div>
<div class="item">
<span class="key">头像</span>
<span class="value">
<img src="{{d.head}}" lay-src="{{d.head}}" layer-src="{{d.head}}" class="layui-inline">
</span>
</div>
<div class="item">
<span class="key">邀请码</span>
<span class="value">{{d.code}}</span>
</div>
<div class="item">
<span class="key">邀请者id</span>
<span class="value">{{d.fatherId}}</span>
</div>
<div class="item">
<span class="key">电话号码</span>
<span class="value">{{d.phone}}</span>
</div>
<div class="item">
<span class="key">钱包</span>
<span class="value">{{d.money}}</span>
</div>
<div class="item">
<span class="key">评星</span>
<span class="value">{{d.score}}</span>
</div>

</div>
<div class="layui-col-md3">
<div class="item">
<span class="key">微信绑定</span>
<span class="value">{{d.openidWx}}</span>
</div>
<div class="item">
<span class="key">QQ绑定</span>
<span class="value">{{d.openidQq}}</span>
</div>
<div class="item">
<span class="key">认证类型</span>
<span class="value">{{d.authType}}</span>
</div>
<div class="item">
<span class="key">认证状态</span>
<span class="value">{{d.auth}}</span>
</div>
<div class="item">
<span class="key">用户状态</span>
<span class="value">{{d.status}}</span>
</div>
<div class="item">
<span class="key">最新登录时间</span>
<span class="value">{{d.loginTime}}</span>
</div>
<div class="item">
<span class="key">创建时间</span>
<span class="value">{{d.createTime}}</span>
</div>
<div class="item">
<span class="key">
<a class="layui-btn layui-btn-normal layui-btn-xs" id="editUserInfo">编辑</a>
</span>
</div>
</div>
</script>
</div>
function getUserInfo(){
mcfish.get('user/getUserInfo',{uid:uid},function(res){
//模板渲染
var getTpl = userInfo.innerHTML
,view = document.getElementById('userInfoView');
res.data.money = mcfish.toMoney(res.data.money);
res.data.openidWx = res.data.openidWx?"已绑定":'未绑定';
res.data.openidQq = res.data.openidQq?"已绑定":'未绑定';
if(res.data.authType){
res.data.authType = res.data.authType===0?"个人认证":'公司认证';
}else{
res.data.authType = "";
}
if(res.data.auth === 0){
res.data.auth = '未认证'
}else if(res.data.auth === 1){
res.data.auth = '认证中'
}else if(res.data.auth === 2){
res.data.auth = '认证通过'
}else if(res.data.auth === 3){
res.data.auth = '认证未通过'
}
res.data.status = res.data.authType===0?"正常":'冻结';
res.data.loginTime = util.toDateString(res.data.loginTime);
res.data.createTime = util.toDateString(res.data.createTime);
laytpl(getTpl).render(res.data, function (html) {
view.innerHTML = html;
});
});
}

注:mcfish.get()为自己封装的ajax,用来请求数据的,在第二次及以后请求 getUserInfo这个方法时便会报错,希望大神能给出解决方案,谢谢大家[微笑]
回帖
  • 因为你执行tpl解析之后把userInfoView里面的html全部替换成解析之后的代码,然而你原先却把模板语句放在这个userInfoView内部,那么考虑一个问题,解析一次之后那个模板语句还存在吗?
    0 回复
  • @岁月小偷 一语惊醒梦中人[good]
    0 回复
  • @岁月小偷 我咋就没想到这点[晕]
    0 回复
  • 把模板放到div外边吧,不然就被覆盖了,找不到dom节点
    0 回复