Re: [問題] jQuery each --> Native Loop

看板Ajax作者 (自立而後立人。)時間13年前 (2012/07/31 22:20), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/3 (看更多)
※ 引述《DRLai (蘇打)》之銘言: : 最近在撰寫一個網頁 : 由於網頁中會動態產生大量表格資料 : 導致網頁會慢一些 : 翻了一下網路上關於 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 我是不覺得這樣會有很明顯的改善啦,不過要換的話也不難 : $("#test").children("div").each(function(){ : $("#debug").append("I got "+$(this).attr("id")+"<br/>"); : }); 改 var item = $("#test").children("div"),dbg = $("#debug"); for(var i = 0,len = item.length;i < len ;++i){ dbg.append("I got "+ item.eq(i).attr("id")+"<br/>"); } 大約是這樣,順手打得可能有些typo 但應該相去不遠。 -- 網頁上拉近距離的幫手 實現 GMail豐富應用的功臣 數也數不清的友善使用者體驗 這就是javascript 歡迎同好到 AJAX 板一同討論。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.34.116.11

08/01 00:46, , 1F
成功了,大謝 m(_ _)m
08/01 00:46, 1F
文章代碼(AID): #1G5-cHif (Ajax)
文章代碼(AID): #1G5-cHif (Ajax)