简体   繁体   English

jQuery反转prependTo命令

[英]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. 我有一个小问题,我在div中有一些静态内容,我需要向其中添加一些额外的内容,prependTo效果很好,但是新内容是在现有内容之后出现的。 appendTo comes before the exisiting content, but each new appended content comes before the previous append. appendTo位于现有内容之前,但是每个新附加内容都位于前一个附加内容之前。

Hard to explain, so I added a little example here: http://jsfiddle.net/9j958/ 很难解释,所以我在这里添加了一个小例子: http : //jsfiddle.net/9j958/

The foo# order is wrong, as you can see. 如您所见,foo#命令是错误的。 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/ 更简单的方法是在fieldset的开头添加一个空div并将新元素附加到该fieldset ,而不是直接在该fieldsethttp : //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/ 或者,如果由于某种原因您不想更改HTML,则可以保留对添加的最后一个元素的引用,并使用insertAfter追加下一个元素: http : //jsfiddle.net/9j958/10 /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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