How do I move an HTML element in jQuery?

后端 未结 3 1535
一生所求
一生所求 2020-12-23 19:55

My HTML structure is like this:

Some content
Some content
<
相关标签:
3条回答
  • 2020-12-23 20:13

    Ever thought about using jQuery UI Sortable ?

    0 讨论(0)
  • 2020-12-23 20:25

    Simply do:

    $('#1').before($('#2'));
    
    0 讨论(0)
  • 2020-12-23 20:30

    You can use .insertBefore(), like this:

    $("#2").insertBefore("#1");
    

    Or, .prependTo(), like this:

    $("#2").prependTo("#parent");
    

    ...or the reverse using #1 and .insertAfter() and .appendTo()...or several other ways actually, it just depends what you're actually after, the above 2 methods should be about the shortest possible though, given 2 IDs.

    I'm assuming this is just an example, remember to use IDs that don't start with a number in an actual HTML4 page, they're invalid and cause several issues.

    0 讨论(0)
提交回复
热议问题