js模板引擎——artTemplate的使用問題

來源:互聯網
上載者:User

標籤:資料   ext   val   htm   name   document   ntb   inner   select   

最近在使用模板引擎,對於出現的錯誤做一個小總結。

想要在下拉框中使用模板,讓下拉選項中的每一項都由模板顯示。

用下面的寫法會出錯,只有一個下拉選項,所有的資料都在一行內。

....<select>    <option id="temp"></option>    <script type="text/html" id="test">          {{if data.count > 0}}                {{each data.list as item i}}                        {{item.id}}-{{item.name}}                 {{/each}}           {{/if}}    </script>    <script src="js/artTemplate.js"></script>    <script>           var data = {                   count:2,                   list:[                         {                             id:1,                             name: "hihi"                         },                         {                             id:2,                             name:"boe"                          }                    ]             }             var html = template("test",data);             document.getElementById("temp").innerHTML = html;    </script></select>

這是模板使用錯誤,正確的方式:

....<!--顯示模板的地方 --><select id="temp></select><!-- 需要引入artTemplate.js才能使用模板--><script src="js/artTemplate.js"></script>  <!-- 使用一個type="text/html"的script標籤存放模板-->    <script type="text/html" id="test">          <option>請選擇</option>          {{if data.count > 0}}                {{each data.list as item i}}                        <option value="{{item.id}}">{{item.name}}</option>                 {{/each}}           {{/if}}    </script> <!-- 渲染模板-->    <script>           var data = {                   count:2,                   list:[                         {                             id:1,                             name: "hihi"                         },                         {                             id:2,                             name:"boe"                          }                    ]             }             var html = template("test",data);             document.getElementById("temp").innerHTML = html;    </script>

當然模板也可以結合ajax和json來使用

js模板引擎——artTemplate的使用問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.