jquery實現(xiàn)列表上下移動功能
廢話少說,我們開始進(jìn)入主題。
今天我們實現(xiàn)的是一個列表頁面上移、下移功能。如圖:
當(dāng)勾選列表中的列時,點擊上移或者下移,會動態(tài)上移或者下移。
html代碼如下:
<div> <input type="button" onclick="up();" value=" 上移 "> <input type="button" onclick="down();" value=" 下移 "> </div> <div> <table width="400px" height="200" class="mytable" cellpadding="5" cellspacing="0"> <tr> <td>序號</td> <td>名字</td> <td>性別</td> </tr> <tr> <td><input type="checkbox" id="c1"/>1</td> <td>小一</td> <td>男</td> </tr> <tr> <td><input type="checkbox" id="c2"/>2</td> <td>小二</td> <td>女</td> </tr> <tr> <td><input type="checkbox" id="c3"/>3</td> <td>小三</td> <td>女</td> </tr> </table> lt;/div>
我們定義一個css樣式叫做mytable
.mytable td,.mytable{ font-size:12px; color:red; border:1px solid #000; text-align:center; border-collapse:collapse; }
然后實現(xiàn)up(),down()方法既可,代碼如下:
$.each($("table input:checked"),function(){ var onthis=$(this).parent().parent(); var getUp=onthis.prev(); if ($(getUp).has("input").size()==0) { alert("頂級元素不能上移"); return; } $(onthis).after(getUp); }); } function down(){ $.each($("table input:checked"),function(){ var onthis=$(this).parent().parent(); var getdown=onthis.next(); $(getdown).after(onthis); }); }
利用jquery提供的函數(shù),實現(xiàn)很簡單,當(dāng)然如果想實現(xiàn)多列同時上移下移,只需要加一個循環(huán)既可,核心代碼就是上邊的。
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)jquery程序設(shè)計有所幫助。
相關(guān)文章
鋒利的jQuery 要點歸納(三) jQuery中的事件和動畫(上:事件篇)
鋒利的jQuery 要點歸納 jQuery中的事件和動畫(上:事件篇)2010-03-03解析Jquery的LigerUI如何實現(xiàn)文件上傳
本篇文章是對Jquery中的LigerUI實現(xiàn)文件上傳的方法,進(jìn)行了分析介紹,需要的朋友可以參考下2013-07-07jQuery?v3.3.1的BUG以及解決辦法(附解決方案)
這篇文章描述了我們?FineUIPro?產(chǎn)品?更新中遇到的一個問題,最終將問題定位到?jQuery.position()?函數(shù),雖然jQuery的做法是依照HTML規(guī)范來的,但是?jQuery.offsetParent()?和?jQuery.position()?兩個函數(shù)有沖突,并且會導(dǎo)致之前的jQuery插件出錯,應(yīng)該算是一個BUG吧2023-03-03web前端開發(fā)JQuery常用實例代碼片段(50個)
本文給大家展示50個jquery代碼片段,這些代碼能夠給你的javascript項目提供幫助,需要的朋友快來學(xué)習(xí)一下吧2015-08-08jQuery常用事件方法mouseenter+mouseleave+hover
這篇文章主要介紹了jQuery常用事件方法mouseenter、mouseleave和hover方法,下文內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下2022-03-03