通过 JQuery 加载和卸载 JS 文件的最佳方法

Best way to load and unload JS file via JQuery(通过 JQuery 加载和卸载 JS 文件的最佳方法)
本文介绍了通过 JQuery 加载和卸载 JS 文件的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在寻找通过 jQuery 加载和卸载一些 JS 文件的最佳方法而感到沮丧,这是我能做的最后一件事:

I've been frustated trying to find the best way to load and unload some JS file via jQuery, this was last what I can do:

  $("#button").live("click", function(){
    var pl = $(this).attr('rel');
    $.getScript('' + siteAddress + 'min/?js=fjs'+ pl +'', function() {
       $('#container').load(""+ siteAddress +"load/"+ pl +"/");    
    });
  });

我想要做的是通过jQuery加载一些页面,同时它会为当前加载的页面包含适当的外部JS文件,它第一次工作正常,但是当我再次单击按钮时,最后一个JS仍然加载,所以它会在同一页面触发两次JS文件中的函数.

What I want to do is to load some page via jQuery, and at same time it will include appropriate external JS file for current page that been loaded, it work fine for the first time, but when I click the button again, the last JS still loaded, so it will trigger the function inside JS file twice time for same page.

我一直在尝试使用 .append,也可以通过更改 <script> 属性并动态创建 <script> 元素,但我得到的都是一样的结果.

I've been try use .append, also by change <script> attribute and create dynamicaly <script> element but still, all i got is same result.

推荐答案

你不能卸载"JavaScript.一旦它被加载,它就被加载了.无法撤消.

You can't "unload" JavaScript. Once it's loaded, it's loaded. There's no undo.

但是,您可以分离事件处理程序.请参阅:http://api.jquery.com/unbind/

However, you can detach event handlers. See: http://api.jquery.com/unbind/

live()unbind() 的一个特例.实时事件处理程序附加到 document 而不是元素,因此您必须像这样删除处理程序:

live() is a special case for unbind(), though. Live event handlers are attached to document rather than the element, so you have to remove the handler like so:

$(document).unbind('click');

但是,这可能会删除更多的处理程序,而不仅仅是一个有问题的处理程序,因此您可以执行以下两种操作之一:1) 命名您的处理程序函数或 2) 创建一个命名空间.

However, that would probably remove more handlers than just the one in question, so you can do one of two things: 1) name your handler function or 2) create a namespace.

命名处理函数

function myClickHandler(){
    var pl = $(this).attr('rel');
    $.getScript('' + siteAddress + 'min/?js=fjs'+ pl +'', function() {
       $('#container').load(""+ siteAddress +"load/"+ pl +"/");    
    });
}

$("#button").live("click", myClickHandler);

// And later ...

$(document).unbind('click', myClickHandler);

命名空间

$("#button").live("click.myNamespace", function(){
    var pl = $(this).attr('rel');
    $.getScript('' + siteAddress + 'min/?js=fjs'+ pl +'', function() {
       $('#container').load(""+ siteAddress +"load/"+ pl +"/");    
    });
});

// And later...

$(document).unbind('click.myNamespace');

更新:

正如@RTPMatt 下面提到的,die() 实际上更合适.上面描述的方法会起作用,但是 die() 更容易使用.但是,使用 die() 您必须将选择器与调用 live() 时使用的选择器完全匹配,否则结果可能无法预测:

As @RTPMatt mentions below, die() is actually more appropriate. The method described above will work, but die() is easier to use. However, with die() you must match the selector exactly to the one used when you called live() or the results may be unpredictable:

$("#button").live("click", function(){
    var pl = $(this).attr('rel');
    $.getScript('' + siteAddress + 'min/?js=fjs'+ pl +'', function() {
       $('#container').load(""+ siteAddress +"load/"+ pl +"/");    
    });
});

// And later ...

$('#button').die('click');

这篇关于通过 JQuery 加载和卸载 JS 文件的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)