全面解析DOM操作和jQuery實(shí)現(xiàn)選項(xiàng)移動(dòng)操作代碼分享
DOM:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-"> <title>DOM選項(xiàng)移動(dòng)操作</title> <style> select { width: px; height: px; } div { display: inline-block; width: px } </style> </head> <body> <select id="unsel" size="" multiple><option>Argentina</option><option>Brazil</option><option>Canada</option><option>Chile</option><option>China</option><option>Cuba</option><option>Denmark</option><option>Egypt</option><option>France</option><option>Greece</option><option>Spain</option></select> <div> <button onclick="move(this.innerHTML)">>></button> <button onclick="move(this.innerHTML)">></button> <button onclick="move(this.innerHTML)"><</button> <button onclick="move(this.innerHTML)"><<</button> </div> <select id="sel" size="" multiple> </select> <script> function $(id){ return document.getElementById(id); } var unsel=null;//保存所有備選國家列表 var sel=[];//保存已選中的國家列表 window.onload=function(){ unsel=$("unsel").innerHTML .replace(/<\/?option>/g," ") .match(/\b[a-zA-Z]+\b/g); } function move(inner){ switch (inner){ case ">>"://全部右移 sel=sel.concat(unsel); unsel.length=; sel.sort(); break; case "<<"://全部左移 unsel=unsel.concat(sel); sel.length=; unsel.sort(); break; case ">"://選中項(xiàng)右移 var opts=document.querySelectorAll("#unsel option"); //從后向前遍歷每個(gè)option for(var i=opts.length-;i>=;i--){ if(opts[i].selected){ //刪除unsel中i位置的個(gè)元素,直接壓入sel sel.push(unsel.splice(i,)[]); } } sel.sort(); break; case "<"://選中項(xiàng)左移 var opts=document.querySelectorAll("#sel option"); for(var i=opts.length-;i>=;i--){ if(opts[i].selected){ unsel.push(sel.splice(i,)[]); } } unsel.sort(); break; } show(); } function show(){//將兩個(gè)數(shù)組,更新到select元素中 $("unsel").innerHTML="<option>" +unsel.join("</option><option>") +"</option>"; $("sel").innerHTML="<option>" +sel.join("</option><option>") +"</option>"; } </script> </body> </html>
jquery:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-"> <title>選項(xiàng)移動(dòng)操作</title> <script src="jquery.min.js"></script> <style> select { width: px; height: px; } div { display: inline-block; width: px } </style> </head> <body> <select id="first" size="" multiple> <option>Argentina</option> <option>Brazil</option> <option>Canada</option> <option>Chile</option> <option>China</option> <option>Cuba</option> <option>Denmark</option> <option>Egypt</option> <option>France</option> <option>Greece</option> <option>Spain</option> </select> <div> <button id="add">></button> <button id="add_all">>></button> <button id="remove"><</button> <button id="remove_all"><<</button> </div> <select id="second" size="" multiple> </select> <script> $("#add").click(function(){ // 將左邊被選中的選項(xiàng),移到右邊去 $("#first>option:selected").appendTo($("#second")); }); $("#add_all").click(function(){ $("#first>option").appendTo($("#second")); }); $("#remove").click(function(){ $("#second>option:selected").appendTo($("#first")); }); $("#remove_all").click(function(){ $("#second>option").appendTo($("#first")); }); // 雙擊事件 $("#first").dblclick(function(){ $("#first>option:selected").appendTo($("#second")); }); $("#second").dblclick(function(){ $("#second>option:selected").appendTo($("#first")); }); </script> </body> </html>
以上所述是小編給大家介紹的DOM操作和jQuery實(shí)現(xiàn)選項(xiàng)移動(dòng)操作代碼分享的全部內(nèi)容,希望對(duì)大家有所幫助!
相關(guān)文章
BootStrap和jQuery相結(jié)合實(shí)現(xiàn)可編輯表格
這篇文章主要介紹了BootStrap和jQuery相結(jié)合實(shí)現(xiàn)可編輯表格的相關(guān)資料,需要的朋友可以參考下2016-04-04基于jquery的設(shè)置頁面文本框 只能輸入數(shù)字的實(shí)現(xiàn)代碼
之前寫過的方法有缺陷,可以輸入空格?,F(xiàn)在將空格也屏蔽了。就是在之前的代碼里加入了過濾空格的功能。2011-04-04修改jQuery.Autocomplete插件 支持中文輸入法 避免TAB、ENTER鍵失效、導(dǎo)致表單提交
jQuery.Autocomplete 是jquery的流行插件,能夠很好的實(shí)現(xiàn)輸入框的自動(dòng)完成(autocomplete)、建議提示(input suggest)功能,支持ajax數(shù)據(jù)加載。2009-10-10JQuery判斷checkbox是否選中及其它復(fù)選框操作方法合集
這篇文章主要介紹了JQuery判斷checkbox是否選中及其它復(fù)選框操作方法合集,本文匯總了網(wǎng)上解決這個(gè)問題比較好的幾篇文章,需要的朋友可以參考下2015-06-06jQuery實(shí)現(xiàn)當(dāng)前頁面標(biāo)簽高亮顯示的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)當(dāng)前頁面標(biāo)簽高亮顯示的方法,涉及jQuery通過自定義函數(shù)操作css樣式的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03jquery css實(shí)現(xiàn)郵箱自動(dòng)補(bǔ)全
這篇文章主要為大家詳細(xì)介紹了jquery css實(shí)現(xiàn)郵箱自動(dòng)補(bǔ)全功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11jquery統(tǒng)計(jì)輸入文字的個(gè)數(shù)并對(duì)其進(jìn)行判斷
判斷輸入文字個(gè)數(shù)并提示還可以輸入多少個(gè)字,類似的功能使用jquery便可輕松實(shí)現(xiàn)2014-01-01jQuery實(shí)現(xiàn)返回頂部功能適合不支持js的瀏覽器
a標(biāo)簽指向錨點(diǎn)top,可以在頂部防止一個(gè)a name=top的錨點(diǎn),這樣在瀏覽器不支持js時(shí)也可以實(shí)現(xiàn)返回頂部的效果了2014-08-08