开发者

Sorting list with jQuery

开发者 https://www.devze.com 2022-12-21 16:21 出处:网络
I\'ve got a small problem I\'m struggling to solve. Let\'s say I\'ve got an unordered list, like: <ul>

I've got a small problem I'm struggling to solve. Let's say I've got an unordered list, like:

<ul> 
    <li> //first
       <div id="div1> text </div>
    </li>

    <li> //second
       <div id="div2> text </div>
    </li>

    <li> //third
       <div id="div3> text </div>
    </li>
&l开发者_高级运维t;/ul>

Is there an easy approach to change the order of this list? So, let's say, the third one would be displayed in the middle? The problem is I've got a lot of stuff under each

  • and can't modify the list on the server-side since I don't have access to logic files :/

    Thanks regards


    You can do it by using jQueryUI. there's a very nice example here


    As long as you know the positions you want to go in and move around you can. Here's an example:

    In this case remove the :eq(2) (third, 0 based) element, then insert it after the first.

    $(document).ready(function(){
      $("ul li:eq(2)").remove().insertAfter($("ul li:eq(0)"));
    });
    
    <ul> 
        <li>
           <div id="div1"> first </div>
        </li>
        <li>
           <div id="div2"> second </div>
        </li>
        <li>
           <div id="div3"> third </div>
        </li>
    </ul>
    

    There are a few other options as well, but the answer is yes you can do this.


    The TinySort plugin should sort you out [TinySort]

  • 0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号