options.add(opt , index)后,原options中的各项的index如何变化?
如果原来的options中有5项,索引值index分别等于0-4。
现在添加opt到第一项。例如:options.add(opt , 3);
如果remove的话,index如何变化?
顺便问一句,如果按option.text.length将 这5项重新按由小到大的顺序重新排列代码怎么写?
高手指教指教! 谢谢了
------解决方案--------------------如果是 add,则插入点以后滴索引自动加 1;
如果是 remove,则删除点以后滴索引自动减 1!
------解决方案--------------------L@_@K
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<title> options sort </title>
<meta name= "generator " content= "editplus " />
<meta name= "author " content= "yixianggao@126.com " />
<meta name= "keywords " content= "javascript " />
<meta name= "description " content= "for javascript region of csdn " />
</head>
<body>
<select id= "cats " size= "1 ">
<option value= "1 "> calico_haha </option>
<option value= "2 "> tortie </option>
<option value= "3 "> siamese </option>
<option value= "4 "> heimao </option>
<option value= "5 "> jiafeimao </option>
</select>
<input type= "button " id= "btnSort " value= "Sort " /> <br />
<pre> calico_haha
tortie
siamese
heimao
jiafeimao </pre>
<script type= "text/javascript ">
<!--
var oSort = document.getElementById( "btnSort ");
oSort.onclick = function() {
var oSel = document.getElementById( "cats ");
var isExchange;
for (var i=1; i <oSel.options.length; i++)
{
isExchange = false;
for (var j=oSel.options.length-1; j> =i; j--)
{
if (oSel.options[j].innerText.length < oSel.options[j-1].innerText.length)
{
oSel.options[j].swapNode(oSel.options[j-1]);
isExchange = true;
}
}
if (!isExchange)
{
break;
}
}
oSel.options[0].selected = true;
};
//-->
</script>
</body>
</html>
------解决方案--------------------反序,只要一个循环
<select name= "aaa ">
<option value= "111 "> 111 </option>
<option value= "222 "> 222 </option>
<option value= "333 "> 333 </option>
<option value= "444 "> 444 </option>
<option value= "555 "> 555 </option>
</select>
<script language=javascript>
var obj=document.getElementById( "aaa ")
var temp=new Array()
for(var i=obj.options.length-1;i> -1;i--)
{
obj.appendChild(obj.options[i])
}
</script>