<div id="div1">
<select id="select1" multiple="multiple">
<option>猪肉option>
<option>羊肉option>
<option>牛肉option>
<option>鸡肉option>
<option>鸭肉option>
select>
<br />
<input type="button" value="添加到右边" onclick="goRight()"/>
<input type="button" value="全部添加到右边" onclick="goRightAll()"/>
div>
<div id="div2">
<select id="select2" multiple="multiple">
<option>白菜option>
<option>卷心菜option>
<option>生菜option>
select>
<br />
<input type="button" value="添加到左边" onclick="goLeft()"/>
<input type="button" value="全部添加到左边" onclick="goLeftAll()"/>
div>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<style type="text/css">
div{
width: 120px;
height: 200px;
float: left;
}
select{
width: 100px;
height: 150px;
}
style>
head>
<body>
<div id="div1">
<select id="select1" multiple="multiple">
<option>猪肉option>
<option>羊肉option>
<option>牛肉option>
<option>鸡肉option>
<option>鸭肉option>
select>
<br />
<input type="button" value="添加到右边" onclick="goRight()"/>
<input type="button" value="全部添加到右边" onclick="goRightAll()"/>
div>
<div id="div2">
<select id="select2" multiple="multiple">
<option>白菜option>
<option>卷心菜option>
<option>生菜option>
select>
<br />
<input type="button" value="添加到左边" onclick="goLeft()"/>
<input type="button" value="全部添加到左边" onclick="goLeftAll()"/>
div>
<script type="text/javascript">
//添加到右边
function goRight(){
var sel2 = document.getElementById("select2"); //获取select2
var options = select1.getElementsByTagName("option");//获取select1的option 返回一个数组
for(var i = 0;i < options.length;i++){//遍历数组
if(options[i].selected){//判断是否被选中
sel2.appendChild(options[i]);//将被选中的项剪切到select2
i--;//每执行一次appendChild,options的length都会减一,为了能实现遍历全部元素,需每次i--
}
}
}
//全部添加到右边
function goRightAll(){
var sel2 = document.getElementById("select2");//获取select2
var options = select1.getElementsByTagName("option");//获取select1的option
for(var i = 0;i < options.length;i++){//遍历options数组
sel2.appendChild(options[i]);//将每个option剪切到select2
i--;
}
}
//添加到左边
function goLeft(){
var sel1 = document.getElementById("select1"); //获取select1
var options = select2.getElementsByTagName("option");//获取select2的option 返回一个数组
for(var i = 0;i < options.length;i++){//遍历数组
if(options[i].selected){//判断是否被选中
sel1.appendChild(options[i]);//将被选中的项剪切到select1
i--;
}
}
}
//全部添加到左边
function goLeftAll(){
var sel1 = document.getElementById("select1");//获取select1
var options = select2.getElementsByTagName("option");//获取select2的option
for(var i = 0;i < options.length;i++){//遍历options数组
sel1.appendChild(options[i]);//将每个option剪切到select1
i--;
}
}
script>
body>
html>
思路
此案例会用到onchange事件
onchange 事件会在域的内容改变时发生,可用于单选框与复选框改变后触发的事件。
//this.value表示当前选中的内容。若复选框选中猪肉时,即this.value == 猪肉
<select id="select1" onchange="changeFood(this.value)">
<option>--请选择--option>
<option>猪肉option>
<option>羊肉option>
<option>牛肉option>
<option>鸡肉option>
select>
var arr = new Array(4);
arr[0] = ["猪肉","红烧肉","回锅肉","梅干菜扣肉","水煮肉片"];
arr[1] = ["羊肉","红烧羊肉","孜然羊肉","干锅羊肉","红焖羊肉"];
for(var i = 0;i < arr.length;i++){
if(arr[i][0] == obj){//判断选中的obj与二维数组中第一列的元素是否匹配
for(var j = 1;j < arr[i].length;j++){
var option1 = document.createElement("option");//创建option标签
var test = document.createTextNode(arr[i][j]);//创建文本
option1.appendChild(test);//把文本添加进option1里面
var value = document.getElementById("select2");//获取select2标签
value.appendChild(option1);//把option添加进value中
}
}
}
//1、获取select2里的option
var options = select2.getElementsByTagName("option");
//遍历options
for(var i = 0;i < options.length;i++){
select2.removeChild(options[i]);//通过父结点删除每一个option
i--;
}
完整代码
<html>
<head>
<meta charset="UTF-8">
<title>title>
head>
<body>
<select id="select1" onchange="changeFood(this.value)">
<option>--请选择--option>
<option>猪肉option>
<option>羊肉option>
<option>牛肉option>
<option>鸡肉option>
select>
<select id="select2">
select>
body>
<script type="text/javascript">
//创建二维数组
var arr = new Array(4);
arr[0] = ["猪肉","红烧肉","回锅肉","梅干菜扣肉","水煮肉片"];
arr[1] = ["羊肉","红烧羊肉","孜然羊肉","干锅羊肉","红焖羊肉"];
arr[2] = ["牛肉","西红柿炖牛肉","水煮牛肉","小炒牛肉","葱爆牛肉","牙签牛肉"];
arr[3] = ["鸡肉","白切鸡","黄焖鸡","咸焗鸡"];
function changeFood(obj){
//清空select2
//1、获取select2里的option
var options = select2.getElementsByTagName("option");
//遍历options
for(var i = 0;i < options.length;i++){
select2.removeChild(options[i]);//通过父结点删除每一个option
i--;
}
//遍历二维数组
for(var i = 0;i < arr.length;i++){
if(arr[i][0] == obj){//判断选中的obj与二维数组中第一列的元素是否匹配
for(var j = 1;j < arr[i].length;j++){
var option1 = document.createElement("option");//创建option标签
var test = document.createTextNode(arr[i][j]);//创建文本
option1.appendChild(test);//把文本添加进option1里面
var value = document.getElementById("select2");//获取select2标签
value.appendChild(option1);//把option添加进value中
}
}
}
}
script>
html>
row:<input type="text" id="row" />
col:<input type="text" id="col" />
<br />
<input type="button" value="create" onclick="create()"/>
<div id="nav">div>
function create(){
//用一个变量存储table及里面的所有标签内容
var tab = "";
//获取文本框的行和列的值
var row = document.getElementById("row").value;
var col = document.getElementById("col").value;
//循环生成行
for(var i = 0;i < row;i++){
tab += "";
//循环生成单元格
for(var j = 0;j < col;j++){
tab += "";
tab += "单元格";
tab += " ";
}
tab += " ";
}
tab += "
";
//将tab添加进div中
//获取div
var nav = document.getElementById("nav");
nav.innerHTML = tab;
}
完整代码
<html>
<head>
<meta charset="UTF-8">
<title>title>
head>
<body>
row:<input type="text" id="row" />
col:<input type="text" id="col" />
<br />
<input type="button" value="create" onclick="create()"/>
<div id="nav">div>
body>
<script type="text/javascript">
function create(){
//用一个变量存储table及里面的所有标签内容
var tab = "";
//获取文本框的行和列的值
var row = document.getElementById("row").value;
var col = document.getElementById("col").value;
//循环生成行
for(var i = 0;i < row;i++){
tab += "";
//循环生成单元格
for(var j = 0;j < col;j++){
tab += "";
tab += "单元格";
tab += " ";
}
tab += " ";
}
tab += "
";
//将tab添加进div中
//获取div
var nav = document.getElementById("nav");
nav.innerHTML = tab;
}
script>
html>