作者webberhan (练习多"多益"善)
看板Web_Design
标题[心得] Expand Your Options in a SELECT Element
时间Tue Nov 21 11:42:25 2006
FROM:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/perf/dhtmlperf.asp
An exception to our earlier rule about using the HTML text methods is when you add a large number of OPTION elements to a SELECT element. It is more efficient to use the innerHTML property than to call the createElement method to access the options collection.
Tip 5: Use innerHTML to add a large number of options to a SELECT element.
Use string concatenation to build up the SELECT element HTML text, then use this to set the innerHTML property. For very large numbers of options, string concatenation can also affect performance. In this situation, build an array and call the Microsoft JScript join method to perform a final concatenation of the OPTION element HTML text.
Show Me
Slow:
var opt;
divUpdate.innerHTML = "<SELECT ID='selUpdate'></SELECT>";
for (var i=0; i<1000; i++)
{
opt = document.createElement( "OPTION" );
selUpdate.options.add( opt );
opt.innerText = "Item " + i;
}
Fast:
var str="<SELECT ID='selUpdate'>";
for (var i=0; i<1000; i++)
{
str += "<OPTION>Item " + i + "</OPTION>";
}
str += "</SELECT>";
divUpdate.innerHTML = str;
Faster:
var arr = new Array(1000);
for (var i=0; i<1000; i++)
{
arr[i] = "<OPTION>Item " + i + "</OPTION>";
}
divUpdate.innerHTML = "<SELECT ID='selUpdate'>" + arr.join() + "</SELECT>";
--
唐 李商隐 无题
相见时难别亦难,东风无力百花残。春蚕到死丝方尽,蜡炬成灰泪始乾。
晓镜但愁云鬓改,夜吟应觉月光寒。蓬山此去无多路,青鸟殷勤为探看。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 125.229.164.118
1F:→ terrybob:有人可以翻中译版吗? = =" 11/21 11:49