jQuery清除DOM以前加载的脚本 [英] jQuery clear DOM previously loaded scripts

查看:106
本文介绍了jQuery清除DOM以前加载的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好日子,我正在一个项目中,我使用jQuery将页面加载到div中,每个页面都有自己的jQuery脚本,我的问题是先前加载的脚本不会从DOM清除,并且会产生更大的问题对于我的项目。



例如,我在一个页面中有一组定时器,它自动保存数据,但是当我更改页面时,计时器仍然运行并弄乱



我管理如下更改的页面:

  $ (document).ready(function(){

$('li')。click(function(event){

$('。progressbarone')。hide );
var theitem = this.id;
// isf ajax加载addon
var thefile =isf_ajax.php?addon =+ this.id;
var progress ='< span class =progressbarone>< img src ='
+ THEMEURL
+'/gfx/images/progessbar1.gifwidth =20height =16 />< / span>';

// pre a
$('。'+ theitem +'_ item')
.append(progress)
.children(':last')
.hide()
.fadeIn(1000);
$('li')。removeClass('active');
$(this).addClass('active');

//加载页
$('#childpage')。remove();
$('#officetable')。html('< div id =childpage> loading< / div>');
$('#childpage')。load(thefile,function(){

//加载动作
$('。progressbarone')fadeOut(2000);
});
});
});

正如你可以看到我有两个div,一个叫做office表来处理子页面,子页面有更改页面的内容。但是,您可以看到我已经使用了remove();功能以及之后,我将子页面放置到可以正式发布的文件的内容,但仍然不会删除我以前加载的脚本。



谢谢你们

解决方案

将脚本附加到页面后,会运行这些脚本。虽然您从DOM中删除脚本标记,但仍将保留变量。我的意思是计时器,函数,代码中声明的任何种类的变量。



所以你可能想要清除以前不再需要的状态。

  function doTheCleanup(){
clearTimeout(globalTimerVariable);
删除someGloablVar;
//等等
}


Good day people, I am working on a project where i load pages into divs using jQuery, and each page has its own jQuery scripts, my problem is that the previously loaded scripts are not clearing from DOM and that creates a bigger problem for my project.

For example, I have a set of timers in one page where it auto saves data but when I change the page the timers are still running and messing up the contents.

I manage changing pages like this:

$(document).ready(function() {

   $('li').click(function (event) {

      $('.progressbarone').hide();
      var theitem = this.id;
      //isf ajax to load with addon
      var thefile = "isf_ajax.php?addon="+this.id;
      var progress = '<span class="progressbarone"><img src="'
         + THEMEURL
         + '/gfx/images/progessbar1.gif" width="20" height="16" /></span>';

      // pre actions
      $('.'+ theitem +'_item')
         .append(progress)
         .children(':last')
         .hide()
         .fadeIn(1000);
      $('li').removeClass('active');
      $(this).addClass('active');

      // load the page
      $('#childpage').remove();
      $('#officetable').html('<div id="childpage">loading</div>');
      $('#childpage').load(thefile, function() {

         // after load actions
         $('.progressbarone').fadeOut(2000);
      });
   });
});

As you can see I have two divs, one is called office table that handles the childpage and childpage has the contents for changed pages. however as you can see I have used the remove(); function as well and after that I placed the childpage to officetable and posted the contents of the file, but still it doesn't remove my previously loaded scripts.

Thank you guys.

解决方案

Once you append your scripts to the page, those are run. Although you remove the scripts tags from the DOM, the variables will remain. By that I mean timers, functions, any kind of variable declared in your code.

So you might want to clear the previous state which is not desired anymore.

function doTheCleanup(){
    clearTimeout(globalTimerVariable);
    delete someGloablVar;
    //and so on
}

这篇关于jQuery清除DOM以前加载的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆