jquery-tmpl 外掛程式

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   os   io   資料   

做項目時頁面上有處功能是:在頁面有處列表、有添加,我添加修改或刪除後要重新整理這個列表,首先想到的是局部重新整理,但我們一般說的局部重新整理就是利於ajax去後台調用資料並顯示,而這裡是一整個列表就比較麻煩了,重新整理重新載入列表時不止要載入列表,還需把table的標籤這些都得重新載入下,寫在js裡或後台都得寫 "<table class=\"datalist\">" 這之類的,太麻煩了 而後期要修改也不好改,於是乎就找到了jquery的tmpl外掛程式,這個貌似還是官方在好幾年已經不維護並停留在了beta版,不過還是有它的好用之處,下面源碼執行個體說明:(有好幾種用法,下面只說明本人用的感覺比較簡單的一種)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script><script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script><script type="text/javascript">    $(function () {        var temp = [            { Name: "blue", explain: "藍色" },            { Name: "red", explain: "紅色" },            { Name: "yellow", explain: "黃色" }        ];        $("#tempTemplate").tmpl(temp).appendTo("#tempList");    })</script></head><body>        <ul id="tempList"></ul>        <script id="tempTemplate" type="text/x-jquery-tmpl">            <li>                <b>${Name}</b> (${explain})            </li>        </script></body></html>
View Code

其實原理很簡單,就好比把寫好的標籤當作模版,用模版出現的關鍵字去迴圈出裡面的資料再顯示出來。

我要重新整理資料的話就用ajax從後台讀取下資料並json格式傳回即可。

外掛程式官方:https://github.com/BorisMoore/jquery-tmpl

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.