我正在通過jquery/ajax/php獲取相當多的json數據。當我得到數據時,我會彈出一個模式顯示在一些表格中。我這樣做就是這樣。將Ajax數據放入html並顯示在頁面上的最佳方法
str1 = "<table class='line_item_order_info_table'>";
str1 += "<tr><td class='line_item_label' >";
str1 += "Order no.";
str1 += "</td></tr>";
str1 += "<tr><td class='line_item_data' id='order_number_td'>";
str1 += data.order_number;
str1 += "</td></tr>";
str1 += "</table>";
str1 += "</td>";
str1 += "<td>";
str1 += "<table class='line_item_order_info_table'>";
str1 += "<tr><td class='line_item_label'>";
str1 += "Order date";
str1 += "</td></tr>";
str1 += "<tr><td class='line_item_data'>";
str1 += data.order_date;
str1 += "</td></tr>";
str1 += "</table>";
//many, many more lines that look like this
$('#modal-div').html(str1);
這是最好的方式把這個數據通過JavaScript的HTML?像這樣跟蹤很難。這麼多的html,沒有任何地方的任何縮進,使它更容易閱讀,是有點痛苦的看看或試圖找到的東西英寸
感謝,如果你能指出我的方向更好的戰略!謝謝!
你有沒有考慮過使用模板? (這個問題有幾個模板引擎的例子http://stackoverflow.com/questions/4286181/what-is-the-best-current-javascript-templating-engine) – MilkyWayJoe
這聽起來像一個糟糕的實現設計。你應該使用@MilkyWayJoe建議的模板。 –
謝謝。你能給我一些模板背景嗎?一個快速的谷歌搜索給了我很多,但沒有很多介紹他們做什麼或爲什麼他們更好或更實際的我在做什麼。也許你知道一些網站.. – 1252748