首页 > web前端 > js教程 > 正文

JavaScript控制listbox列表框的项目上下移动的方法_javascript技巧

php中文网
发布: 2016-05-16 16:08:52
原创
1379人浏览过

本文实例讲述了javascript控制listbox列表框的项目上下移动的方法。分享给大家供大家参考。具体分析如下:

这段JS代码可以控制listbox内的元素向上或者向下移动,这个功能非常有用。下面是详细的代码

复制代码 代码如下:
function listbox_move(listID, direction) {
    var listbox = document.getElementById(listID);
    var selIndex = listbox.selectedIndex;
    if(-1 == selIndex) {
        alert("Please select an option to move.");
        return;
    }
    var increment = -1;
    if(direction == 'up')
        increment = -1;
    else
        increment = 1;
    if((selIndex + increment)         (selIndex + increment) > (listbox.options.length-1)) {
        return;
    }
    var selValue = listbox.options[selIndex].value;
    var selText = listbox.options[selIndex].text;
    listbox.options[selIndex].value = listbox.options[selIndex + increment].value
    listbox.options[selIndex].text = listbox.options[selIndex + increment].text
    listbox.options[selIndex + increment].value = selValue;
    listbox.options[selIndex + increment].text = selText;
    listbox.selectedIndex = selIndex + increment;
}
//..
//..
listbox_move('countryList', 'up'); //move up the selected option
listbox_move('countryList', 'down'); //move down the selected option

下面是详细的演示代码,可以在浏览器内使用
复制代码 代码如下:
Click below buttons to select or deselect all options from select box.

   



<script><br /> function listboxMove(listID, direction) {<br /> var listbox = document.getElementById(listID);<br /> var selIndex = listbox.selectedIndex;<br /> if(-1 == selIndex) {<br /> alert("Please select an option to move.");<br /> return;<br /> }<br /> var increment = -1;<br /> if(direction == 'up')<br /> increment = -1;<br /> else<br /> increment = 1;<br /> if((selIndex + increment) < 0 ||<br /> (selIndex + increment) > (listbox.options.length-1)) {<br /> return;<br /> }<br /> var selValue = listbox.options[selIndex].value;<br /> var selText = listbox.options[selIndex].text;<br /> listbox.options[selIndex].value = listbox.options[selIndex + increment].value<br /> listbox.options[selIndex].text = listbox.options[selIndex + increment].text<br /> listbox.options[selIndex + increment].value = selValue;<br /> listbox.options[selIndex + increment].text = selText;<br /> listbox.selectedIndex = selIndex + increment;<br /> }<br /> </script>

希望本文所述对大家的javascript程序设计有所帮助。

Upscale
Upscale

AI图片放大工具

Upscale 85
查看详情 Upscale
相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号