Added support for .before(), .after(), and .replaceWith() on disconnected DOM nodes. Fixes bug #3940.

This commit is contained in:
John Resig 2009-09-14 22:09:42 +00:00
parent bca8225413
commit 173c1477ae
2 changed files with 40 additions and 10 deletions

View file

@ -106,15 +106,29 @@ jQuery.fn.extend({
},
before: function() {
return this.domManip(arguments, false, function(elem){
this.parentNode.insertBefore( elem, this );
});
if ( this[0] && this[0].parentNode ) {
return this.domManip(arguments, false, function(elem){
this.parentNode.insertBefore( elem, this );
});
} else {
var set = jQuery.isFunction(arguments[0]) ?
jQuery( arguments[0]() ) :
jQuery.apply(jQuery, arguments);
return this.pushStack( set.add( this ), "before", arguments );
}
},
after: function() {
return this.domManip(arguments, false, function(elem){
this.parentNode.insertBefore( elem, this.nextSibling );
});
if ( this[0] && this[0].parentNode ) {
return this.domManip(arguments, false, function(elem){
this.parentNode.insertBefore( elem, this.nextSibling );
});
} else {
return jQuery.isFunction(arguments[0]) ?
this.add( arguments[0]() ) :
this.add.apply( this, arguments );
}
},
clone: function( events ) {
@ -193,7 +207,11 @@ jQuery.fn.extend({
},
replaceWith: function( value ) {
return this.after( value ).remove();
if ( this[0] && this[0].parentNode ) {
return this.after( value ).remove();
} else {
return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
}
},
detach: function( selector ) {