日期:2014-05-16 浏览次数:20535 次
var op = document.createElement("option"); op.value = i; op.innerHTML = i; oSelect.appendChild(op);
oSelect.options.add(new Option(i, i));
oSelect.options.length = iLength; for (var i = 0; i < iLength; i++) { oSelect.options[i].text = oSelect.options[i].value = iStart + i; }
<select id="testSel"></select> <div id="testShow"></div> <input name="" type="button" value="效率测试" id="testBtn" /> <script> document.getElementById("testBtn").onclick = function(){ var testSel = document.getElementById("testSel"), testShow = document.getElementById("testShow"), iMin = 0, iMax = 1000, dt; testShow.innerHTML = ""; dt = new Date().getTime(); var len = iMax - iMin + 1; testSel.options.length = len; for (var i = 0; i < len; i++) { testSel.options[i].text = testSel.options[i].value = iMin + i; } testShow.innerHTML += "笨方法:" + (new Date().getTime() - dt) + " 毫秒;"; dt = new Date().getTime(); testSel.options.length = 0; for (var i = iMin; i <= iMax; i++) { testSel.options.add(new Option(i, i)); } testShow.innerHTML += "new Option方法:" + (new Date().getTime() - dt) + " 毫秒;"; dt = new Date().getTime(); testSel.options.length = 0; for (var i = iMin; i <= iMax; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; testSel.appendChild(op); } testShow.innerHTML += "dom方法:" + (new Date().getTime() - dt) + " 毫秒;"; } </script>
oSelect.selectedIndex = iIndex;
<select id="idSelected"></select> <input name="" type="button" value="默认项测试" id="testBtnSelected" /> <script> document.getElementById("testBtnSelected").onclick = function(){ var oSelect = document.getElementById("idSelected"); oSelect.options.length = 0; for (var i = 0; i <= 10; i++) { var op = document.createElement("option"); op.value = op.innerHTML = i; oSelect.appendChild(op); } oSelect.selectedIndex = 5; } </script>