开发者

jQuery .before() re-executing javascript

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜