Created
November 21, 2011 23:16
-
-
Save re5et/1384314 to your computer and use it in GitHub Desktop.
jQuery detach, work, reattach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.dwr = function(fn){ | |
return this.each(function() { | |
var $this = $(this); | |
var tmpElement = $('<div/>'); | |
$(this).after(tmpElement); | |
$this.detach(); | |
fn.call(this); | |
tmpElement.replaceWith($this); | |
}); | |
}; | |
})(jQuery); | |
// makes element detach / work on / reattach easier. | |
// detaching an element, doing your work, and then reattaching | |
// provides significant performance benefits. | |
// ~ 250ms | |
// $('#main').dwr(function(){ | |
// var $this = $(this); | |
// for(i=0;i<10000;i++){ | |
// $this.toggleClass('foo'); | |
// } | |
// }); | |
// ~ 900ms | |
// var main = $('#main'); | |
// for(i=0;i<10000;i++){ | |
// main.toggleClass('foo'); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment