Moving listbox left and right based on javascript_javascript skills
The example in this article explains the detailed code of JavaScript to realize the left and right movement of listbox, and shares it with you for your reference. The specific content is as follows
Rendering:
Specific code:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>listbox左右移动</title> </head> <body> <div style="background-color:#CCC; width:450px; height:300px; margin:150px,0,0,450px; border:1px solid"> <table align="center" width="285" height="169" bgcolor="#99CCFF"> <tr> <td width="100"> <select name="first" id="first" size="10" multiple="multiple" style="background-color:#3FC;"> <option value="选项1">选项1</option> <option value="选项2">选项2</option> <option value="选项3">选项3</option> <option value="选项4">选项4</option> <option value="选项5">选项5</option> <option value="选项6">选项6</option> <option value="选项7">选项7</option> <option value="选项8">选项8</option> </select> </td> <td width="85" valign="middle"> <input name="add" id="add" type="button" value="--->"/> <input name="add_all" id="add_all" type="button" value="===>"/> <input name="remove" id="remove" type="button" value="<---"/> <input name="remove_all" id="remove_all" type="button" value="<==="/> </td> <td width="100" align="left"> <select name="second" id="second" size="10" multiple="multiple" style="background-color:#3FC;"> <option value="选项9">选项9</option> </select> </td> </tr> </table> </div> </body> <script type="text/javascript"> //左移右 /*<input name="add" id="add" type="button" value="--->"/>*/ document.getElementById("add").onclick = function add() { var firstSel = document.getElementById("first"); var option = firstSel.getElementsByTagName("option"); //javascript的数组是动态数组,长度是可以变的。 //所以先取得下拉列表的长度,避免option被移走后长度变小,导致后面循环终止,出现beg var oplength=option.length; var secondSel = document.getElementById("second"); for(i=0;i<oplength;i++) { /* selectedIndex: 该下标返回下拉列表的索引值 注: 如果有多个被选中的情况下,永远返回第一个选中的索引值,索引最小的那个 如果没有被选中的情况下,返回-1 selectedIndex是<select>的属性 */ if(firstSel.selectedIndex!=-1) { secondSel.appendChild(option[firstSel.selectedIndex]); } } } /*<input name="add_all" id="add_all" type="button" value="===>"/>*/ document.getElementById("add_all").onclick = function addAll() { var firstSel = document.getElementById("first"); var option = firstSel.getElementsByTagName("option"); //javascript的数组是动态数组,长度是可以变的。 //所以先取得下拉列表的长度,避免option被移走后长度变小,导致后面循环终止,出现beg var oplength=option.length; var secondSel = document.getElementById("second"); for(i=0;i<oplength;i++) { /*因为javascript的数组是动态数组,长度是可以变的。所以当移走全部把数 组的值移走(一个一个的移走,数组长度马上-1,所以数组下标也是-1.因次我们要把每次移的是走下标为0的那个 数,这样才保证可以全部移走)*/ secondSel.appendChild(option[0]); } } /*双击后把option移到右边*/ document.getElementById("first").ondblclick = function dblclick() { /*方法一*/ /* var firstSel = document.getElementById("first"); var option = firstSel.getElementsByTagName("option"); //javascript的数组是动态数组,长度是可以变的。 //所以先取得下拉列表的长度,避免option被移走后长度变小,导致后面循环终止,出现beg var oplength=option.length; var secondSel = document.getElementById("second"); for(i=0;i<oplength;i++) { //双击可以看成:第一次点击选中,第二次点击移动 secondSel.appendChild(option[firstSel.selectedIndex]); } */ /*方法二*/ /* this: this表示document.getElementById("first") 下拉列表 this.selectedIndex表示下拉列表选中的项 */ var secondSel = document.getElementById("second"); secondSel.appendChild(this[this.selectedIndex]); } //右移左 /*<input name="remove" id="remove" type="button" value="<---"/>*/ document.getElementById("remove").onclick = function remove() { var secondSel = document.getElementById("second"); var firstSel = document.getElementById("first"); var option = secondSel.getElementsByTagName("option"); //javascript的数组是动态数组,长度是可以变的。 //所以先取得下拉列表的长度,避免option被移走后长度变小,导致后面循环终止,出现beg var oplength=option.length; for(i=0;i<oplength;i++) { /* selectedIndex: 该下标返回下拉列表的索引值 注: 如果有多个被选中的情况下,永远返回第一个选中的索引值,索引最小的那个 如果没有被选中的情况下,返回-1 selectedIndex是<select>的属性 */ if(secondSel.selectedIndex!=-1) { firstSel.appendChild(option[secondSel.selectedIndex]); } } } /*<input name="remove_all" id="remove_all" type="button" value="<==="/>*/ document.getElementById("remove_all").onclick = function remove_all() { var secondSel = document.getElementById("second"); var firstSel = document.getElementById("first"); var option = secondSel.getElementsByTagName("option"); //javascript的数组是动态数组,长度是可以变的。 //所以先取得下拉列表的长度,避免option被移走后长度变小,导致后面循环终止,出现beg var oplength=option.length; for(i=0;i<oplength;i++) { /*因为javascript的数组是动态数组,长度是可以变的。所以当移走全部把数 组的值移走(一个一个的移走,数组长度马上-1,所以数组下标也是-1.因次我们要把每次移的是走下标为0的那个 数,这样才保证可以全部移走)*/ firstSel.appendChild(option[0]); } } /*双击后把option移到右边*/ document.getElementById("second").ondblclick = function dblclick() { /*方法一*/ /* var secondSel = document.getElementById("second"); var firstSel = document.getElementById("first"); var option = secondSel.getElementsByTagName("option"); //javascript的数组是动态数组,长度是可以变的。 //所以先取得下拉列表的长度,避免option被移走后长度变小,导致后面循环终止,出现beg var oplength=option.length; for(i=0;i<oplength;i++) { //双击可以看成:第一次点击选中,第二次点击移动 firstSel.appendChild(option[secondSel.selectedIndex]); } */ /*方法二*/ /* this: this表示document.getElementById("second") 下拉列表 this.selectedIndex表示下拉列表选中的项 */ var firstSel = document.getElementById("first"); firstSel.appendChild(this[this.selectedIndex]); } </script> </html>
The code comments are very detailed, I hope it can help everyone.
The above is the entire content of this article. I hope it will be helpful to everyone in learning javascript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
