$('script').last().remove();`//remove last file from html file
$('head').append('<script src=""></script>'); //add js file
使用$ .getScript,我可以即时加载js文件。是否可以“卸载”其中的某些文件?
我有一个基于Ajax的管理面板。我有3个部分。单击每个部分时,我只想加载与之关联的文件。切换到另一部分时,我要卸载不必要的文件。
“正在卸载”,如果有[[真正可能,在这里就没有多大意义了。让我们退后一步,看看为什么不加载所有以以下内容开头的文件:
已经
<script>
并不会删除它声明的功能,它附加的事件处理程序等。总之,为什么
您要卸载吗?如果要消除影响,那将是不容易的,无论如何也不是通用的。如果要简化页面...在这里没有任何好处,实际上您将只承担删除元素的成本,而对于JavaScript方面的客户端却没有好处。然而,如果有正当的理由这样做。我会将所有已加载脚本的代码放在一个类中。当我想卸载它时,我只是将其分配为null。例如。
//external file:
o1=new Object();
o1.function1=function(){
//blah blah blah
};
o1.function2=function(){
//blah blah blah
};
o1.var1=1;
//and if when i'll need to get rid of all of this i can just
o1=null;
假定
I.m.h.o。加载到页面
x.htm + x.css + x.js中的某个
$('script').last().remove();`//remove last file from html file
$('head').append('<script src=""></script>'); //add js file
在这种情况下,我将使所有JavaScript代码成为对象结构的一部分,然后将对象设置为null。
var myObj = { "property1": true, "property2": "Hello my name is Sam.", "mynamedfunction": function(){ //do stuff }, "othernamedfunction": function(){ //do other stuff } };
完成一次,设置myObj = null;因此,不是加载和卸载文件,而是根据需要加载和卸载对象!因此,您将加载包含对象的文件,但是一旦完成处理后将其设置为null,则甚至可以在销毁该对象后再次将其加载。
$('script').last().remove();`//remove last file from html file
$('head').append('<script src=""></script>'); //add js file
var myObj = {
"property1": true,
"property2": "Hello my name is Sam.",
"mynamedfunction": function(){
//do stuff
},
"othernamedfunction": function(){
//do other stuff
}
};
完成一次,设置myObj = null;因此,不是加载和卸载文件,而是根据需要加载和卸载对象!因此,您将加载包含对象的文件,但是一旦完成处理后将其设置为null,则甚至可以在销毁该对象后再次将其加载。