I have a piece of code that is using .before() and the code it is moving has inline javascript which .before() re-开发者_Python百科executes. Is there a way to prevent .before() form re-executing javascript code?
You could take the element that you're .before()
ing and strip script tags from it. (untested)
$(newHTML).find('script').remove().end().insertBefore(whereToInsert);
Other than moving the javascript out of the HTML that you're relocating, I suppose you could set a flag to true
after the first execution of the code, which it checks when executing.
Maybe you can detach the script, move the elements around, then reattach (if you still need it). Something like this:
var theScript = $('#foo').find('script').detach();
var everythingElse = $('#foo'); // everything minus the script(s)
$('#baz').before( everythingElse ); // Document reflow happens
everythingElse.append( theScript ); // now add it back in?
Not tested, but maybe worth a shot.
精彩评论