当前位置: 首页 > 图文教程 > 网络编程 > Javascript > js 调整select 位置的函数

Javascript
jQuery生成asp.net服务器控件的代码
javascript 实现的完全兼容鼠标滚轴缩放图片的代码
JavaScript学习笔记(十七)js 优化
使用SyntaxHighlighter实现HTML高亮显示代码的方法
javascript contains和compareDocumentPosition 方法来确定是否HTML节点间的关系
利用jQuery 实现GridView异步排序、分页的代码
jquery.lazyload 实现图片延迟加载jquery插件
Lazy Load 延迟加载图片的 jQuery 插件
jquery 插件实现图片延迟加载效果代码
javascript小数计算出现近似值的解决办法
jquery1.4后 jqDrag 拖动 不可用
jquery 应用代码 方便的排序功能
选择TreeView控件的树状数据节点的JS方法(jquery)
jquery 图片Silhouette Fadeins渐显效果
JQuery Dialog(JS 模态窗口,可拖拽的DIV)
javascript 同时在IE和FireFox获取KeyCode的代码
js 键盘记录实现(兼容FireFox和IE)
javascript 函数速查表
jQuery AnythingSlider滑动效果插件
经典海量jQuery插件 大家可以收藏一下

Javascript 中的 js 调整select 位置的函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 191 ::
收藏到网摘: n/a

js 调整select 位置的函数,向上移动,向下移动,移动到最上,移动到最后 这里把项目中写过的几个js函数来给大家分享,功能是通过js来实现对select 中的option的位置进行移动,代码如下 // 排序:向上移动
function Up()
...{
var sel=document.getElementById("selectCheck"); //获取select
var nIndex = sel.selectedIndex; //需要进行操作的select 项的索引
var nLen = sel.length; //select 总共项目数
if ((nLen<1)||(nIndex==0)) return;
if (nIndex<0)
...{
alert("请选择一个要移动的已选按钮!");
return;
}
var sValue=sel.options[nIndex].value;
var sHTML=sel.options[nIndex].innerHTML;
sel.options[nIndex].value=sel.options[nIndex-1].value;
sel.options[nIndex].innerHTML=sel.options[nIndex-1].innerHTML;
sel.options[nIndex-1].value=sValue;
sel.options[nIndex-1].innerHTML=sHTML;
sel.selectedIndex=nIndex-1;
}
// 排序:向下移动
function Down()
...{
var sel=document.getElementById("selectCheck");
var nIndex = sel.selectedIndex;
var nLen = sel.length;
if ((nLen<1)||(nIndex==nLen-1)) return;
if (nIndex<0)
...{
alert("请选择一个要移动的已选按钮!");
return;
}
var sValue=sel.options[nIndex].value;
var sHTML=sel.options[nIndex].innerHTML;
sel.options[nIndex].value=sel.options[nIndex+1].value;
sel.options[nIndex].innerHTML=sel.options[nIndex+1].innerHTML;
sel.options[nIndex+1].value=sValue;
sel.options[nIndex+1].innerHTML=sHTML;
sel.selectedIndex=nIndex+1;
}
//移动到最上
function UpFirst()
...{
var sel=document.getElementById("selectCheck");
var nIndex = sel.selectedIndex;
var nLen = sel.options.length;
if ((nLen<1)||(nIndex==0)) return;
if(nIndex<0)
...{
alert("请选择一个要移动的已选按钮!");
return;
}
var tempValue = document.getElementById("tempValue");//用于临时存放option的值
tempValue.value = "";
for(var k=0;k<nIndex;k++)
...{
tempValue.value += sel.options[k].value+";";
}
var arrValue = tempValue.value.split(';');
var sValue=sel.options[nIndex].value;
var sHTML=sel.options[nIndex].innerHTML;
sel.options[0].value = sValue;
sel.options[0].innerHTML = sHTML;
for(var j=1;j<=nIndex;j++)
...{
sel.options[j].value = arrValue[j-1];
sel.options[j].innerHTML = arrValue[j-1];
}
sel.selectedIndex = 0;
}
//移动到最后
function DownLast()
...{
var sel=document.getElementById("selectCheck");
var nIndex = sel.selectedIndex;
var nLen = sel.options.length;
if ((nLen<1)||(nIndex==nLen)) return;
if(nIndex<0)
...{
alert("请选择一个要移动的已选按钮!");
return;
}
var tempValue = document.getElementById("tempValue");//用于临时存放option的值
tempValue.value = "";
for(var k=nIndex+1;k<nLen;k++)
...{
tempValue.value += sel.options[k].text+";";
}
var arrValue = tempValue.value.split(';');
var sValue=sel.options[nIndex].value;
var sHTML=sel.options[nIndex].innerHTML;

for(var j=nIndex;j<nLen;j++)
...{
sel.options[j].value = arrValue[j-nIndex ];
sel.options[j].innerHTML = arrValue[j-nIndex];
}
sel.options[nLen-1].value = sValue;
sel.options[nLen-1].innerHTML = sHTML;
sel.selectedIndex = nLen-1;
}