Web Front-end
JS Tutorial
Sample code sharing using JavaScript to implement two select drop-down box options to move left and rightThis article mainly introducesJavaScriptThe function of moving the two select drop-down box options left and right. JS realizes the movement of drop-down box elements to each other. It has certain reference value. Interested friends can For reference
I had nothing to do today and I wrote a small program using js that can move elements between two select drop-down boxes. I just started learning javascript and there are still many optimization areas.
Javascript code
<script type="text/javascript">
/**选中的元素向右移动**/
function moveRight()
{
//得到第一个select对象
var selectElement = document.getElementById("first");
var optionElements = selectElement.getElementsByTagName("option");
var len = optionElements.length;
if(!(selectElement.selectedIndex==-1)) //如果没有选择元素,那么selectedIndex就为-1
{
//得到第二个select对象
var selectElement2 = document.getElementById("secend");
// 向右移动
for(var i=0;i<len ;i++)
{
selectElement2.appendChild(optionElements[selectElement.selectedIndex]);
}
} else
{
alert("您还没有选择需要移动的元素!");
}
}
//移动所有的到右边
function moveAll()
{
//得到第一个select对象
var selectElement = document.getElementById("first");
var optionElements = selectElement.getElementsByTagName("option");
var len = optionElements.length;
//alert(len);
//将第一个selected中的数组翻转
var firstOption = new Array();
for(var k=len-1;k>=0;k--)
{
firstOption.push(optionElements[k]);
}
var lens = firstOption.length;
//得到第二个select对象
var selectElement2 = document.getElementById("secend");
for(var j=lens-1;j>=0;j--)
{
selectElement2.appendChild(firstOption[j]);
}
}
//移动选中的元素到左边
function moveLeft()
{
//首先得到第二个select对象
var selectElement = document.getElementById("secend");
var optionElement = selectElement.getElementsByTagName("option");
var len = optionElement.length;
//再次得到第一个元素
if(!(selectElement.selectedIndex==-1))
{
var firstSelectElement = document.getElementById("first");
for(i=0;i<len;i++)
{
firstSelectElement.appendChild(optionElement[selectElement.selectedIndex]);//被选中的那个元素的索引
}
}else
{
alert("您还没有选中要移动的项目!");
}
}
//全部向左移
function moveAllLeft()
{
var selectElement = document.getElementById("secend");
var optionElements = document.getElementsByTagName("option");
var len = optionElements.length;
var optionEls = new Array();
for(var i=len-1;i>=0;i--)
{
optionEls.push(optionElements[i]);
}
var lens = optionEls.length;
var firstSelectElement = document.getElementById("first");
for(var j=lens-1;j>=0;j--)
{
firstSelectElement.appendChild(optionEls[j]);
}
}
</script>The above is the javascript code, and the following is the html plus css code.
Html code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.select_move { margin:0px auto; width:206px; height:140px; margin-top:300px;}
.select_move_1 { float:left;}
.select_move_2 { float:left;}
.select_move_3 { float:left;}
</style>
</head>
<body>
<p class="select_move">
<p class="select_move_1">
<select name="first" size="10" id="first" multiple="multiple">
<option value="宝马">宝马</option>
<option value="丰田">丰田</option>
<option value="奥迪">奥迪</option>
<option value="凯迪拉克">凯迪拉克</option>
<option value="现代">现代</option>
<option value="奔驰">奔驰</option>
<option value="法拉利">法拉利</option>
</select>
</p>
<p class="select_move_2">
<input type="button" value="------>" onclick="moveRight()"/><br />
<input type="button" value="===>" onclick="moveAll()" /><br />
<input type="button" value="<------" onclick="moveLeft()"/><br />
<input type="button" value="<===" onclick="moveAllLeft()"/>
</p>
<p class="select_move_3">
<select size="10" id="secend" multiple="multiple">
</select>
</p>
</p>
</body>
</html>The above is the detailed content of Sample code sharing using JavaScript to implement two select drop-down box options to move left and right. For more information, please follow other related articles on the PHP Chinese website!
Python vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AMPython is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
Python vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AMPython and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
From C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AMThe shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
JavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AMDifferent JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
Beyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AMJavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.
Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AMI built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AMThis article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base
JavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AMJavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor





