我在 JS 中有一个应用程序,我必须使用 AJAX 加载应用程序的一部分。AJAX 功能完成后,执行脚本。
但是当我想加载应用程序的另一部分时,我想知道如何删除绑定到我刚刚加载的应用程序部分的所有事件和变量?
例如 :
$('#loadPart1').click(function(){
$.ajax({
url : url,
success : function(xhr){
// xhr contains javascript to execute
// and events I want to stop executing afterward
$('body').html(xhr);
});
});
// load another part of the app
// should stop execute previous JS
// and now executes just loaded JS from this AJAX load.
$('#loadPart2').click(function(){
$.ajax({
url : url,
success : function(xhr){
// xhr contains javascript to execute
// and replace old script
$('body').html(xhr);
});
});
我希望你明白我要解释什么。这只是一个帮助的代码,这不是我的真实代码,所以不要理会它,它只是一个图像供您理解问题。
因为我已经有一个应用程序,但是当我在 AJAX 中加载某些内容时,之前的脚本事件仍然会执行。
有没有一种技术,一种模式可以阻止我不想要的 Javascript 的执行。我想删除所有事件,但不明确调用它们。