js實現內容顯示並使用json傳輸資料,jsjson
最近做一個項目,要求是div內為空白 所有代碼都寫在<script>裡面,<script>裡面的文本用json傳輸,這個問題對於我來說有些困難。
還好,最後在教程和同事的協助下寫出來了,下面貼上原始碼。
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>測試專案</title><script src="js\newdoT.js"></script>//關鍵點在這裡 引用dot.js這個庫。這個庫需要自己下載<style>#center{text-align: center;}#test{text-align: left;}</style></head><body><div class="mytest" id="mytest"></div><script type="text/javascript">window.onload = function() {var temp = doT.template($("#mytemp").html());//定義一個temp使id為mytemp的標籤可以讀取dot.JS內的內容var mes = doT.template($("#mydot").html());//定義一個mes使id為mydot的標籤可以讀取dot.JS內的內容$("#mytest").append(mes);//將擷取的值追加到mytest這個div中$("#mytest").append(temp);//將擷取的值追加到mytest這個div中{<!--定義一個json-->var json = {"test": "包郵 正品瑪姬兒壓縮面膜無紡布紙膜100粒 送泡瓶面膜刷噴瓶 新款",<!--找到id為test的標籤,使文檔內容=test-->"test1":"包郵 還是快遞費還是快樂放假了;愛上法蘭克福驕傲了卡夫卡拉升"};// var obj = eval(json); <!--使用函數eval將定義的json轉化為js對象-->document.getElementById("test").innerHTML = json.test;<!--輸出值-->document.getElementById("test1").innerHTML=json.test1;}};</script> <script id="mydot" type="text/template"><table class="sui-table table-bordered"><thead><tr><th id="center"> </th><th id="center">新客戶uv</th><th id="center">新客戶轉化率(%)</th><th id="center">老客戶uv</th><th id="center">老客戶轉化率(%)</th></tr></thead><tbody><tr><td id="center">昨日</td><td id="center">1.0</td><td id="center">15</td><td id="center">11</td><td id="center">1.3</td></tr><tr><td id="center">前日</td><td id="center">1.1</td><td id="center">32</td><td id="center">32</td><td id="center">24</td></tr><tr><td id="center">兩天前</td><td id="center">2.0</td><td id="center">24</td><td id="center">12.00</td><td id="center">1.25</td></tr><tr><td id="center">行業平均</td><td id="center">1.30</td><td id="center">21</td><td id="center">1.07</td><td id="center">1.40</td></tr><tr><td id="center">當前現狀</td><td id="center"><span class="sui-label label-warning">低於平均</span</td><td id="center"><span class="sui-label label-info">高於平均</span></td><td id="center"><span class="sui-label label-success">持平</span></td><td id="center"><span class="sui-label label-warning">低於平均</span></td></tr></tbody></table></script><script id="mytemp" type="text/template"><table class="sui-table table-bordered"><thead><tr><th colspan="5"> <label class="checkbox pull-left"><input type="checkbox">訂單編號:7867473872181848</label><span class="pull-right">成交時間:2014-01-11 11:59</span></th></tr></thead><tbody><tr><td width="50%"><div class="typographic"><img src="http://www.bkjia.com/uploads/allimg/160322/011IA031-0.jpg_80x80.jpg"><a href="#" class="block-text"><span id="test"><span></a><span>1</span><ul class="unstyled"><li>申請售後</li><li>投訴賣家</li></ul></div></td><td rowspan="2" width="11%" class="center">56.50</td><td rowspan="2" width="13%" class="center"><ul class="unstyled"><li>交易成功</li><li><a href="#">訂單詳情</a></li><li><a href="#">對方已評</a></li></ul></td><td rowspan="2" width="13%" class="center"><a href="#" class="btn">評價 </a></td><td rowspan="2" width="13%" class="center"><ul class="unstyled"><li><a href="#">刪除</a></li><li><a href="#">標記</a></li><li><a href="#">分享</a></li></ul></td></tr><tr><td width="50%"><div class="typographic"><img src="http://www.bkjia.com/uploads/allimg/160322/011I644b-1.jpg_80x80.jpg"><a id="test1" href="#" class="block-text"></a><span>1</span><ul class="unstyled"><li>申請售後</li><li>投訴賣家</li></ul></div></td></tr></tbody></table></script></body></html>
希望本文所述對大家學習javascript程式設計有所協助。
您可能感興趣的文章:
- ajax.js裡面有內容顯示效果,根據ID
- AJAX 非同步傳輸資料的問題
- asp.net下 jquery jason 高效傳輸資料
- 基於Jquery的跨域傳輸資料(JSONP)
- JavaScript使表單中的內容顯示在螢幕上的方法
- JS實現左右拖動改變內容顯示地區大小的方法
- java編程實現基於UDP協議傳輸資料的方法
- Python爬蟲抓取手機APP的傳輸資料