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>

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