使用jQuery创建拖拽排序列表

Category : JavaScript

项目中需要用到拖拽排序,于是去研究了jQuery的UI,发现它提供了一种非常简单的方式来实现可视化排序.
首先是html代码:

   <ul id="test">
   <li>first</li>
   <li>second</li>
   <li>third</li>
   </ul>
   <input id="test-button" type="button">
   <input id="show" type="text">

然后在script中导入jQuery,ui.core和ui.sortable3个包

   <script type="text/javascript" src="jquery.js"></script>
   <script type="text/javascript" src="ui.core.js"></script>
   <script type="text/javascript" src="ui.sortable.js"></script>

最后,写对应的js脚本

   $(document).ready(function(){
   $('#test').sortable({
   items:'li'
   });
   $('#test-button').click(function(){
   $('#show').attr('value',$('#test').sortable('toArray'));
   });
   });

解释一下:
要排序的东西放在ul里,用sortable()来使列表可排序,items指出要排序的元素,使用sortable('toArray')来获得当前的排序结果.

Tags :  jQuery  JavaScript 

Published by 2008-12-22 16:13:11 | Views : 223 | Comments : 35


Comments

fajyyrf said :

Wc5CaK piejctmhpspo, [url=http://kmdbqfsrfhik.com/]kmdbqfsrfhik[/url], [link=http://xvbruzrvddul.com/]xvbruzrvddul[/link], http://vciaqwcadvwf.com/

Published by : 2009-10-24 17:32:13

Abandschon said :

This in turn puts strain on other muscles that have not been used in that way in the past. rent a car 052188

Published by : 2009-11-21 03:36:14

fontklein1 said :

Acomplia is a diet pill which helps to quit smoking and also help loose weight. order acomplia rqf accutane %-]]] valium online :-[[[

Published by : 2009-12-09 21:21:53

arialfett said :

order acomplia pqvwu order ambien 8-]]] order tramadol 791492

Published by : 2009-12-09 23:37:58

majestic said :

propecia qeoyla prednisone =-] viagra 1833 auto insurance 710684

Published by : 2010-01-15 05:26:11

gunther said :

cialis mvhv order viagra %P meridia tdp levitra jerhnv

Published by : 2010-01-16 06:12:40

web09 said :

buy levitra jrm buy ambien :-] propecia snqrl meridia 861

Published by : 2010-01-19 22:25:18

choose said :

viagra sxnc

Published by : 2010-01-20 04:49:54

rachel said :

home insurance =PPP home insurance auuelj auto insurance 8013

Published by : 2010-01-23 11:23:43

inhoudsopave said :

affects of synarel >:-OOO ambien fda :))) zocor and orange rectum discharge trce what is better cialis or levitra 420839

Published by : 2010-01-25 20:18:52

 *(required)

 (won't be displayed)