久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

通過 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 文件的最佳方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 亚洲欧美日韩中文字幕一区二区三区 | 成人精品国产免费网站 | 亚洲精品区 | 伦理午夜电影免费观看 | 精品视频久久久久久 | 国产资源在线观看 | 免费在线观看黄网站 | 欧美一级免费看 | 精精国产xxxx视频在线野外 | 亚洲成人一区二区 | 欧美a级成人淫片免费看 | 国产成在线观看免费视频 | 亚洲91 | 国产高清精品一区二区三区 | 国产成人av一区二区三区 | 精品国产精品一区二区夜夜嗨 | 中文字幕久久久 | 天堂一区在线观看 | 免费精品一区 | 精品视频一区二区三区四区 | 久久国产精品无码网站 | 欧美一级特黄aaa大片在线观看 | 国产区视频在线观看 | 亚洲欧洲成人在线 | jlzzxxxx18hd护士| 久久久久国产精品 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 特黄av| a在线免费观看视频 | av黄色在线观看 | 亚洲精品久久嫩草网站秘色 | 在线久草 | www.亚洲 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 久久久久久国产免费视网址 | 久久久久99 | 青青久久 | 国产免费黄网 | 亚洲一区二区在线播放 | 久久久久国产 | av毛片|