作者TonyQ (自立而後立人。)
看板Ajax
标题Re: [问题] jQuery each --> Native Loop
时间Tue Jul 31 22:19:59 2012
※ 引述《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
1F:推 DRLai:成功了,大谢 m(_ _)m 08/01 00:46