2013-05-06 80 views
0

我有2個腳本的網站上,我發展,一個與其他如何防止JavaScript文件與另一個文件衝突?

第二個矛盾的,如果第一個是missing.so我不知道是什麼問題,只會工作運行。

你可以看到這個鏈接。

http://electionsguru.in/mfc_new/newkursplan.php?id_shop=35

在有預覽的標題,是在第3節,在這部分,如果你點擊任何標題光中會出現,但是左邊的菜單下拉腳本無法正常工作和相互衝突與燈箱腳本此頁面。

+1

你的問題是關於JavaScript,但你用php和mysql標記? – 2013-05-06 13:09:31

+0

@Jhansi這一個可能會幫助你http://stackoverflow.com/questions/9851642/how-do-i-stop-the-two-jquery-libraries-from-conflicting-with-each-other – snowp 2013-05-06 13:10:58

+0

如果你運行使用Firebug的頁面會在下面的部分中看到一個錯誤(TypeError:$(...).dataTable不是函數): 2013-05-06 13:15:36

回答

1

根據腳本的大小,使用名稱空間通常是消除衝突的好方法。

引進的2層最常見的方法是:

var ns = ns || {}; 
ns.ondocready = function(){/*your declaration here*/}; 
ns.onsomeotherfunct = function(arg){/*your declaration here*/}; 

var othernamespace = { 
    publicvar:'something available to all namespaced functions', 
    ondocready:function(){/*your declaration here*/}, 
    someotherfunction:function(arg){/*your declaration here*/} 
}; 

然後調用命名對象內的功能(例如,命名空間)

ns.ondocready(); 
othernamespace.someotherfunction('test'); 
othernamespace.publicvar = 'available to functions within namespace by referencing with "this" keyword'; 

唯一需要注意的要記住,當使用「this」關鍵字引用對象內的變量或函數時,它可能與jQuery「this」關鍵字衝突。爲了避免,在你的函數中設置一個變量。

var ns1 = { 
    islive : false, 
    docheck : function(){ 
    var ns = this; //--set scope to variable for access to prevent conflict within jQuery functions where this keyword is referenced 
    if(this.islive){/*conditional declaration*/}; 
    } 
}