[問題] jQuery each --> Native Loop

看板Ajax作者 (蘇打)時間13年前 (2012/07/31 22:15), 編輯推噓1(104)
留言5則, 2人參與, 最新討論串1/3 (看更多)
最近在撰寫一個網頁 由於網頁中會動態產生大量表格資料 導致網頁會慢一些 翻了一下網路上關於 jQuery performance 的文章 看到把.each改用 javascript native loop 會快很多 嘗試修改卻改不出來 希望各位高手協助一下 簡單的程式碼如下 <html> <head> <script type="text/javascript" src="jquery.js"></script> </head> <body> <div id="test"> <div id="a1"></div> <div id="a2"></div> </div> <div id="debug"></div> <script> $(document).ready(function(){ $("#test").children("div").each(function(){ $("#debug").append("I got "+$(this).attr("id")+"<br/>"); }); }); </script> </body> </html> 程式會取得 DIV#test 接著把他底下的 DIV id列印出來 想把 $("#test").children("div").each 換掉改用for 這樣有辦法嗎? 感謝m(_ _)m -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.235.199.15

07/31 22:56, , 1F
動態產生表格會慢通常是因為做太多次DOM操作吧,像這例子
07/31 22:56, 1F

07/31 22:57, , 2F
與其取代$.each不如把要append的東西接成大字串一次append
07/31 22:57, 2F

07/31 22:57, , 3F
改進得多
07/31 22:57, 3F

07/31 22:58, , 4F
和原問題無關$(this).attr("id")直接寫this.id就好了?
07/31 22:58, 4F

08/01 00:44, , 5F
append那個部份我原本就有做了,只是範例懶得改^^"
08/01 00:44, 5F
文章代碼(AID): #1G5-X_b1 (Ajax)
文章代碼(AID): #1G5-X_b1 (Ajax)