简体   繁体   中英

jQuery reversing prependTo order

I have a small problem, I have some static content inside a div and I need to add some extra content to it, prependTo works good, but the new content comes after the exisiting one. appendTo comes before the exisiting content, but each new appended content comes before the previous append.

Hard to explain, so I added a little example here: http://jsfiddle.net/9j958/

The foo# order is wrong, as you can see. Any way around this?

Reverse the elements before the each call.

$($('tr').get().reverse()).each(
    ...
);

I'd have two suggestions.

The easier would be to add a empty div to the beginning of your fieldset and append your new elements to that instead of the fieldset directly: http://jsfiddle.net/9j958/9/

Or if for some reason you can't or don't want to change your HTML, then you could keep a reference to the last element you added and append the next element with insertAfter : http://jsfiddle.net/9j958/10/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM