茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端)
    來源:易賢網(wǎng) 閱讀:1159 次 日期:2016-07-08 14:52:51
    溫馨提示:易賢網(wǎng)小編為您整理了“基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端)”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端)的相關(guān)資料,需要的朋友可以參考下

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

    每點擊一個TAB標簽,會請求對應(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)容少點還好,但是當內(nèi)容多起來的時候,再使用這種方式,以后維護起來不方便,也影響美觀,可閱讀性差。

    突然想起來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、準備好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、準備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標簽,就可以顯示數(shù)據(jù)了:

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

    更多信息請查看網(wǎng)絡(luò)編程

    2026上岸·考公考編培訓報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)