js控制select 中option项的上下移动

①select.options("id")方法取出一个option
②证明option的索引不能通过option.index来更改其索引值
③通过option的swapNode方法来交换索引
④选取一个Item
通过select的selectIndex来选中一个option
如果该Item有id,可以通过id值来选取该Item:  options("hong")

 

 

<html>
<body>
<form>
<select name="menu" size="8" onChange="this.selectedIndex=4;">
<option value="0">0</ption>
<option value="1">1</ption>
<option value="2">2</ption>
<option value="3">3</ption>
<option value="4" selected>4</ption>
</select>
<input type="button" value="uporder" οnclick="setOrder(1)"/>
<input type="button" value="downorder" οnclick="setOrder()"/>

</form>
<script>
function setOrder(f){
var sel=document.forms[0].menu;
var seloption=sel.options[sel.selectedIndex];
//alert(seloption.index);
if(f==1){
if((seloption.index-1)>=0)
seloption.swapNode(sel.options[seloption.index-1]);
}
else{
 if((seloption.index+1)<sel.length)
seloption.swapNode(sel.options[seloption.index+1]);
}
}


</script>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章