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

JsRender實用入門教程

 更新時間:2014年10月31日 10:08:18   投稿:shichen2014  
這篇文章主要介紹了JsRender實用入門實例,包含了tag else使用、循環(huán)嵌套訪問父級數(shù)據(jù)等知識點,并提供了完整的實例下載,非常具有實用價值,需要的朋友可以參考下

本文是一篇JsRender的實用入門教程,實例講述了tag else使用、循環(huán)嵌套訪問父級數(shù)據(jù)等知識點。分享給大家供大家參考。具體如下:

前言

JsRender是一款基于jQuery的JavaScript模版引擎,它具有如下特點:

  ·  簡單直觀

  ·  功能強(qiáng)大

  ·  可擴(kuò)展的

  ·  快如閃電

這些特性看起來很厲害,但幾乎每個模版引擎,都會這么宣傳。。。

由于工作需要,小菜才接觸到此款模版引擎。使用了一段時間,發(fā)現(xiàn)它確實比較強(qiáng)大,但小菜覺得有些地方強(qiáng)大的過頭了,反倒讓人覺得很難理解。

另一方面,JsRender的官方文檔比較詳細(xì),但其他資料出奇的少,遇到點什么問題,基本搜不到,不僅僅是相關(guān)問題搜不到,幾乎就是沒有結(jié)果。

再加上JsRender有些地方確實是不好理解,所以急需小菜分享一些“最佳實踐”。

基于最近一段時間的使用,小菜總結(jié)了一些實用經(jīng)驗,當(dāng)然,這些經(jīng)驗在官方文檔上是找不到的。

嵌套循環(huán)使用#parent訪問父級數(shù)據(jù)(不推薦)

復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>嵌套循環(huán)使用#parent訪問父級數(shù)據(jù) --- by 楊元</title>
    <style>
    </style>
   
  </head>
  <body>
   
    <div>
      <table>
        <thead>
          <tr>
            <th width="10%">序號</th>
            <th width="10%">姓名</th>
            <th width="80%">家庭成員</th>
          </tr>
        </thead>
        <tbody id="list">
         
        </tbody>
      </table>
    </div>
   
    <script src="jquery.min.js"></script>
    <script src="jsviews.js"></script>
   
    <!-- 定義JsRender模版 -->
    <script id="testTmpl" type="text/x-jsrender">
      <tr>
        <td>{{:#index + 1}}</td>
        <td>{{:name}}</td>
        <td>
          {{for family}}
            {{!-- 利用#parent訪問父級index --}}
            <b>{{:#parent.parent.index + 1}}.{{:#index + 1}}</b>
            {{!-- 利用#parent訪問父級數(shù)據(jù),父級數(shù)據(jù)保存在data屬性中 --}}
            {{!-- #data相當(dāng)于this --}}
            {{:#parent.parent.data.name}}的{{:#data}}
          {{/for}}
        </td>
      </tr>
    </script>
   
    <script>
      //數(shù)據(jù)源
      var dataSrouce = [{
        name: "張三",
        family: [
          "爸爸",
          "媽媽",
          "哥哥"
        ]
      },{
        name: "李四",
        family: [
          "爺爺",
          "奶奶",
          "叔叔"
        ]
      }];
     
      //渲染數(shù)據(jù)
      var html = $("#testTmpl").render(dataSrouce);
      $("#list").append(html);
     
    </script>
   
  </body>
</html>

嵌套循環(huán)使用參數(shù)訪問父級數(shù)據(jù)(推薦)

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>嵌套循環(huán)使用參數(shù)訪問父級數(shù)據(jù) --- by 楊元</title>
    <style>
    </style>
   
  </head>
  <body>
   
    <div>
      <table>
        <thead>
          <tr>
            <th width="10%">序號</th>
            <th width="10%">姓名</th>
            <th width="80%">家庭成員</th>
          </tr>
        </thead>
        <tbody id="list">
         
        </tbody>
      </table>
    </div>
   
    <script src="jquery.min.js"></script>
    <script src="jsviews.js"></script>
   
    <!-- 定義JsRender模版 -->
    <script id="testTmpl" type="text/x-jsrender">
      <tr>
        <td>{{:#index + 1}}</td>
        <td>{{:name}}</td>
        <td>
          {{!-- 使用for循環(huán)時,可以在后邊添加參數(shù),參數(shù)必須以~開頭,多個參數(shù)用空格分隔 --}}
          {{!-- 通過參數(shù),我們緩存了父級的數(shù)據(jù),在子循環(huán)中通過訪問參數(shù),就可以間接訪問父級數(shù)據(jù) --}}
          {{for family ~parentIndex=#index ~parentName=name}}
            <b>{{:~parentIndex + 1}}.{{:#index + 1}}</b>
            {{!-- #data相當(dāng)于this --}}
            {{:~parentName}}的{{:#data}}
          {{/for}}
        </td>
      </tr>
    </script>
   
    <script>
      //數(shù)據(jù)源
      var dataSrouce = [{
        name: "張三",
        family: [
          "爸爸",
          "媽媽",
          "哥哥"
        ]
      },{
        name: "李四",
        family: [
          "爺爺",
          "奶奶",
          "叔叔"
        ]
      }];
     
      //渲染數(shù)據(jù)
      var html = $("#testTmpl").render(dataSrouce);
      $("#list").append(html);
     
    </script>
   
  </body>
</html>

自定義標(biāo)簽(custom tag)中使用else(強(qiáng)烈不推薦)

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>自定義標(biāo)簽中使用else --- by 楊元</title>
    <style>
    </style>
   
  </head>
  <body>
   
    <div>
      <table>
        <thead>
          <tr>
            <th width="50%">名稱</th>
            <th width="50%">單價</th>
          </tr>
        </thead>
        <tbody id="list">
         
        </tbody>
      </table>
    </div>
   
    <script src="jquery.min.js"></script>
    <script src="jsviews.js"></script>
   
    <!-- 定義JsRender模版 -->
    <script id="testTmpl" type="text/x-jsrender">
      <tr>
        <td>{{:name}}</td>
        <td>
          {{!-- isShow為自定義標(biāo)簽,price是傳入的參數(shù),status是附加屬性 --}}
          {{isShow price status=0}}
            {{:price}}
          {{else price status=1}}
            --
          {{/isShow}}
        </td>
      </tr>
    </script>
   
    <script>
      //數(shù)據(jù)源
      var dataSrouce = [{
        name: "蘋果",
        price: 108
      },{
        name: "鴨梨",
        price: 370
      },{
        name: "桃子",
        price: 99
      },{
        name: "菠蘿",
        price: 371
      },{
        name: "橘子",
        price: 153
      }];
     
      //自定義標(biāo)簽
      $.views.tags({
        "isShow": function(price){
          var temp=price+''.split('');
         
          if(this.tagCtx.props.status === 0){
            //判斷價格是否為水仙花數(shù),如果是,則顯示,否則不顯示
            if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){
              return this.tagCtxs[0].render();
            }else{
              return this.tagCtxs[1].render();
            }
          }else{
            return "";
          }
         
        }
      });
     

      //渲染數(shù)據(jù)
      var html = $("#testTmpl").render(dataSrouce);
      $("#list").append(html);
     
    </script>
   
  </body>
</html>

用helper代替自定義標(biāo)簽(推薦)

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>用helper代替自定義標(biāo)簽 --- by 楊元</title>
    <style>
    </style>
   
  </head>
  <body>
   
    <div>
      <table>
        <thead>
          <tr>
            <th width="50%">名稱</th>
            <th width="50%">單價</th>
          </tr>
        </thead>
        <tbody id="list">
         
        </tbody>
      </table>
    </div>
   
    <script src="jquery.min.js"></script>
    <script src="jsviews.js"></script>
   
    <!-- 定義JsRender模版 -->
    <script id="testTmpl" type="text/x-jsrender">
      <tr>
        <td>{{:name}}</td>
        <td>
          {{!-- 利用原生的if做分支跳轉(zhuǎn),利用helper做邏輯處理 --}}
          {{if ~isShow(price)}}
            {{:price}}
          {{else}}
            --
          {{/if}}
        </td>
      </tr>
    </script>
   
    <script>
      //數(shù)據(jù)源
      var dataSrouce = [{
        name: "蘋果",
        price: 108
      },{
        name: "鴨梨",
        price: 370
      },{
        name: "桃子",
        price: 99
      },{
        name: "菠蘿",
        price: 371
      },{
        name: "橘子",
        price: 153
      }];
     
      //Helper
      $.views.helpers({
        "isShow": function(price){
          var temp=price+''.split('');
          if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){
            return true;
          }else{
            return false;
          }
        }
      });

      //渲染數(shù)據(jù)
      var html = $("#testTmpl").render(dataSrouce);
      $("#list").append(html);
     
    </script>
   
  </body>
</html>

完整實例代碼點擊此處本站下載

希望本文所述對大家JsRender程序設(shè)計的學(xué)習(xí)有所幫助。

相關(guān)文章

  • JS中l(wèi)et的基本用法舉例

    JS中l(wèi)et的基本用法舉例

    ES6新增了let命令,用來聲明變量,它的用法類似于var,但是所聲明的變量,只在let命令所在的代碼塊內(nèi)有效,下面這篇文章主要給大家介紹了關(guān)于JS中l(wèi)et的基本用法,需要的朋友可以參考下
    2023-01-01
  • canvas時鐘效果

    canvas時鐘效果

    本文主要介紹了canvas實現(xiàn)時鐘效果的代碼。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • JS如何根據(jù)當(dāng)前日期獲取一周所有日期

    JS如何根據(jù)當(dāng)前日期獲取一周所有日期

    這篇文章主要介紹了JS如何根據(jù)當(dāng)前日期獲取一周所有日期,文中通過實例代碼補(bǔ)充介紹了js獲取當(dāng)前一周的時間,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • 利用原生的JavaScript實現(xiàn)簡單拼圖游戲

    利用原生的JavaScript實現(xiàn)簡單拼圖游戲

    拼圖游戲是我們大家都玩過的一款小游戲,下面這篇文章主要給大家介紹了關(guān)于如何利用原生的JavaScript實現(xiàn)簡單拼圖游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-11-11
  • javascript和php使用ajax通信傳遞JSON的實例

    javascript和php使用ajax通信傳遞JSON的實例

    今天小編就為大家分享一篇javascript和php使用ajax通信傳遞JSON的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 原生JS實現(xiàn)貪吃蛇小游戲

    原生JS實現(xiàn)貪吃蛇小游戲

    這篇文章主要為大家詳細(xì)介紹了原生JS貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • JS遍歷Json字符串中鍵值對先轉(zhuǎn)成JSON對象再遍歷

    JS遍歷Json字符串中鍵值對先轉(zhuǎn)成JSON對象再遍歷

    這篇文章主要介紹了JS遍歷Json字符串中鍵值對的方法,先將Json字符串轉(zhuǎn)換成JSON對象,再進(jìn)行遍歷,需要的朋友可以參考下
    2014-08-08
  • 一篇文章帶你從零快速上手Rollup

    一篇文章帶你從零快速上手Rollup

    這篇文章主要給大家介紹了如何通過一篇文章快速從零快速上手Rollup的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 微信小程序?qū)崿F(xiàn)日歷功能

    微信小程序?qū)崿F(xiàn)日歷功能

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)日歷功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • js實現(xiàn)網(wǎng)頁的兩個input標(biāo)簽內(nèi)的數(shù)值加減(示例代碼)

    js實現(xiàn)網(wǎng)頁的兩個input標(biāo)簽內(nèi)的數(shù)值加減(示例代碼)

    下面小編就為大家?guī)硪黄猨s實現(xiàn)網(wǎng)頁的兩個input標(biāo)簽內(nèi)的數(shù)值加減(示例代碼)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論