亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端)

 更新時間:2016年04月06日 14:10:02   作者:飛鴻影~  
這篇文章主要介紹了基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端)的相關(guān)資料,需要的朋友可以參考下

寫本文之前,我正在做一個基于Tab頁的訂單中心:

每點擊一個TAB標(biāo)簽,會請求對應(yīng)狀態(tài)的訂單列表。之前的項目,我會在js里使用 + 連接符連接多個html內(nèi)容:

var html = '';
html += '<div class="empty-list">' + 
'<div class="icon icon-box"></div>'+
'<div>還沒有訂單</div>'+
'</div>';

Html內(nèi)容少點還好,但是當(dāng)內(nèi)容多起來的時候,再使用這種方式,以后維護(hù)起來不方便,也影響美觀,可閱讀性差。

突然想起來PHP模板的相關(guān)知識,那么應(yīng)該也有類似JavaScript模板一說。由于為了快速使用在項目上,就去網(wǎng)上找了找。還真有:

基于HTML模板和JSON數(shù)據(jù)的JavaScript交互

http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/

看了下文章,使用起來還是蠻簡單的:

1、準(zhǔn)備好html模板

<textarea class="js-order-tmp" style="display:none;">
<!--goods-block-->
<div class="block m-s-order">
<div class="block-item block-news">
<a href="<?php echo site_url('order/detail/'. '$id$')?>" class="dis-block external">
<div class="item-img">
<img src="$thumb$"/>
</div>
<div class="item-intro">
<div class="sub-l">
<p class="t-order-num">訂單號:$order_num$</p>
<p class="t-name">收件人:$cus_name$</p>
<p class="t-date">$create_time$</p>
</div>
<div class="sub-r">
<p class="t-expand"><span class="icon icon-right"></span></p>
<p class="t-flag">$flag_name$</p>
</div>
</div>
<div class="clear"></div>
</a>
</div>
</div>
<!--/goods-block-->
</textarea>
<textarea class="js-no-order-tmp" style="display:none;">
<!-- no order -->
<div class="empty-list">
<div class="icon icon-box"></div>
<div>還沒有訂單</div>
</div>
<!-- /no order -->
</textarea> 

其中變量部分全部用 variate variate 表示。

2、模板方法很簡單,直接寫一個基于字符串原型的擴展方法,確保全局可用:

String.prototype.temp = function(obj) {
return this.replace(/\$\w+\$/gi, function(matchs) {
var returns = obj[matchs.replace(/\$/g, "")]; 
return (returns + "") == "undefined"? "": returns;
});
}; 

主要使用到了正則知識。

3、準(zhǔn)備json數(shù)據(jù):

{
"ecd": 0,
"msg": "成功",
"result": [{
"id": "32",
"order_num": "test-001",
"title": "test",
"thumb": "http:\/\/40DA1265-40F6-D622-8BA5-04BA0AF72573.jpg",
"item_id": "21",
"price": "0.11",
"cus_name": "test",
"cus_tel": "10086",
"cus_address": "北京 北京海淀區(qū)",
"flag": "5",
"create_time": "20160329115544",
"update_time": "20160330120001",
"flag_name": "訂單已取消"
}],
"locate": ""
} 

4、使用ajax顯示數(shù)據(jù)

$.progress_show('正在努力加載中');
$.ajax({
url: site_url + 'api/order/getAll/' + status,
type: 'get',
dataType: 'json',
error: doAjax.error,
success: function (response) {
$.progress_hide();
if (response.ecd == '0') {
var htmlList = '', htmlTemp = $("textarea.js-order-tmp").val();
if(typeof response.result === 'undefined'){
htmlList = $("textarea.js-no-order-tmp").val();
}else{
$.each(response.result, function(i,el) {
htmlList += htmlTemp.temp(el);
});
}
$('.js-status-' + status).empty().append(htmlList);
return true;
} else {
return $.alert(response.msg);
}
},
}); 

這里的部分方法沒有給出,大家知道流程、原理即可。通過點擊TAB標(biāo)簽,就可以顯示數(shù)據(jù)了:

以上內(nèi)容是小編給大家介紹的基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端),希望對大家有所幫助!

相關(guān)文章

最新評論