开发者

jQuery insertBefore(':first', object) vs prependTo(object)

Forgive me for being stupid, but are these two effectively the same?

The only reason I ask is I've seen the former in a plugin I'm adapting, and I was a little thrown by the fact it wasn't simply using prependTo() instead;

$('li:last', list)
    .clone()
    .insertBefore( $('li:first', list) );

Update

Following on from my initial question, and concerning @Nick's comments regarding cloning, would these two scenarios produce the same outcome?

// 1
for (i = 1; i <= number; i++) {
    $('li:first', list)
        .clone()
        .insertA开发者_如何学Cfter( $('li:last', list) );
    $('li:first', list).remove();
}

// 2
for (i = 1; i <= number; i++) {
    $('li:first', list).appendTo(list);
}


It depends on the usage.

insertBefore() literally inserts an element before an element whereas prependTo() inserts an element at the beginning (but within) an element.

In your case, $('li:last', list).clone().insertBefore( $('li:first', list) ); will insert the last element before the first li element.

If you were to use $('li:last', list).clone().prependTo( $('li:first', list) );, the last li element would be added inside of the first li element, which you dont want. To use prependTo() in the same manner as insertBefore, you would need to do $('li:last', list).clone().prependTo( $(list) ); (assuming list is a reference to the UL tag.)


Yes, they would accomplish the same thing, as long as you're cloning in both cases..otherwise it would move the element.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜