標籤:click image tor val tle ext ted name rds
如下:
實現功能:點擊第一個按鈕,讓選中的對象從左邊移動到右邊;
點擊第二個按鈕,讓左邊的所有對象移動到右邊;
點擊第三個按鈕,讓選中的對象從右邊邊移動到左邊;
點擊第四個按鈕,讓右邊的所有對象移動到左邊。
存在BUG:點擊第一個或者第三個按鈕,不選擇對象也能讓末位的對象移動到另外一個框中;
選中2個以上的對象,點擊第一或者第三個按鈕,只能移動一個對象到另一邊。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html> <head> <title>建立網頁</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <script type="text/javascript" src="jquery.js"></script> <style type="text/css"> select{width:130px; height:220px;} </style> <script> //$(selector).append(content) :將content追加到selector選取器內部的最後面 // $(content).appendTo(selector):將content追加到selector選取器內部的最後面 $(function () { //當頁面載入完成時; $("input").eq(0).click( function () { //按下第一個按鈕,觸發函數 var n = $("#hebei")[0].selectedIndex; //擷取被選中的option的下標n $("#hebei>option").eq(n).appendTo($("#henan")); //把下標為n的option移動到henan的下拉式清單中 }); $("input").eq(1).click( function () { //按下第二個按鈕,觸發函數 $("#hebei>option").appendTo($("#henan")); //把hebei的option全部移動到henan的下拉式清單中 }); $("input").eq(2).click( function () { //按下第三個按鈕,觸發函數 var n = $("#henan")[0].selectedIndex; //擷取被選中的option的下標n $("#henan>option").eq(n).appendTo($("#hebei")); //把下標為n的option移動到hebei的下拉式清單中 }); $("input").eq(3).click( function () { //按下第四個按鈕,觸發函數 $("#henan>option").appendTo($("#hebei")); //把henan的option全部移動到hebei的下拉式清單中 }); }); </script> </head> <body> <select id="hebei" multiple="multiple"> <option>石家莊</option> <option>保定</option> <option>邯鄲</option> <option>邢台</option> <option>衡水</option> </select> <select id="henan" multiple="multiple"> <option>鄭州</option> <option>開封</option> <option>洛陽</option> <option>周口</option> <option>信陽</option> </select><br /><br /> <input type="button" value="-->"> <input type="button" value="==>" > <input type="button" value="<--" > <input type="button" value="<==" > </body></html>
JQ寫下拉式清單項目移動,還存在2個小BUG(內附和原始碼)