全面解析DOM操作和jQuery实现选项移动操作代码分享
DOM:
<!DOCTYPEhtml> <htmllang="en"> <head> <metacharset="UTF-"> <title>DOM选项移动操作</title> <style> select{ width:px; height:px; } div{ display:inline-block; width:px } </style> </head> <body> <selectid="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> <buttononclick="move(this.innerHTML)">>></button> <buttononclick="move(this.innerHTML)">></button> <buttononclick="move(this.innerHTML)"><</button> <buttononclick="move(this.innerHTML)"><<</button> </div> <selectid="sel"size=""multiple> </select> <script> function$(id){ returndocument.getElementById(id); } varunsel=null;//保存所有备选国家列表 varsel=[];//保存已选中的国家列表 window.onload=function(){ unsel=$("unsel").innerHTML .replace(/<\/?option>/g,"") .match(/\b[a-zA-Z]+\b/g); } functionmove(inner){ switch(inner){ case">>"://全部右移 sel=sel.concat(unsel); unsel.length=; sel.sort(); break; case"<<"://全部左移 unsel=unsel.concat(sel); sel.length=; unsel.sort(); break; case">"://选中项右移 varopts=document.querySelectorAll("#unseloption"); //从后向前遍历每个option for(vari=opts.length-;i>=;i--){ if(opts[i].selected){ //删除unsel中i位置的个元素,直接压入sel sel.push(unsel.splice(i,)[]); } } sel.sort(); break; case"<"://选中项左移 varopts=document.querySelectorAll("#seloption"); for(vari=opts.length-;i>=;i--){ if(opts[i].selected){ unsel.push(sel.splice(i,)[]); } } unsel.sort(); break; } show(); } functionshow(){//将两个数组,更新到select元素中 $("unsel").innerHTML="<option>" +unsel.join("</option><option>") +"</option>"; $("sel").innerHTML="<option>" +sel.join("</option><option>") +"</option>"; } </script> </body> </html>
jquery:
<!DOCTYPEhtml> <htmllang="en"> <head> <metacharset="UTF-"> <title>选项移动操作</title> <scriptsrc="jquery.min.js"></script> <style> select{ width:px; height:px; } div{ display:inline-block; width:px } </style> </head> <body> <selectid="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> <buttonid="add">></button> <buttonid="add_all">>></button> <buttonid="remove"><</button> <buttonid="remove_all"><<</button> </div> <selectid="second"size=""multiple> </select> <script> $("#add").click(function(){ //将左边被选中的选项,移到右边去 $("#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实现选项移动操作代码分享的全部内容,希望对大家有所帮助!