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

来源:互联网 发布:不停弹出windows media 编辑:程序博客网 时间:2024/05/05 12:18

①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" onclick="setOrder(1)"/>
<input type="button" value="downorder" onclick="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>

原创粉丝点击