您的位置:首页 > Web前端 > JQuery

两种方法使用jquery实现左右移动效果(包含each遍历方式)

2012-03-21 21:29 1221 查看
第一种:使用each()遍历方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery实现左右选择器</title>
</head>
<script language="javascript" type="text/javascript" src="../../include/jquery.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function (){
//遍历元素

$("option").each(function (index,domEle){

$(domEle).click(function (){

conStr=$(this);

$("#right").click(function (){

conStr.appendTo(".right");

});

$("#left").click(function (){

conStr.appendTo(".left");

});

});

});

});

</script>

<body>
<table width="200" border="0">
<caption>选项卡</caption>

<tr>

<td><select multiple="multiple" class="left" style="width:100px; height:200px">

<option>苹果</option>

<option>香蕉</option>

<option>草莓</option>

<option>梨</option>

</select>

</td>

<td align="center">

<input type="button" value="向右" id="right"><br><br>

<input type="button" value="向左" id="left"><br><br>

</td>

<td><select multiple="multiple" class="right" style="width:100px; height:200px"></select></td>

</tr>
</table>
</body>
</html>
第二种:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery实现左右选择器</title>
</head>
<script language="javascript" type="text/javascript" src="../../include/jquery.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function (){

//给向右按钮添加onclick事件

$("#right").click(function (){

//选中目标元素追加到左边

$(".left option:selected").appendTo(".right");

});

//给向左按钮添加onclick事件

$("#left").click(function (){

//选中目标元素追加到右边

$(".right option:selected").appendTo(".left");

});

});

</script>

<body>
<table width="200" border="0">
<caption>选项卡</caption>

<tr>

<td><select multiple="multiple" class="left" style="width:100px; height:200px">

<option>苹果</option>

<option>香蕉</option>

<option>草莓</option>

<option>梨</option>

</select>

</td>

<td align="center">

<input type="button" value="向右" id="right"><br><br>

<input type="button" value="向左" id="left"><br><br>

</td>

<td><select multiple="multiple" class="right" style="width:100px; height:200px"></select></td>

</tr>
</table>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: