根據腳本的大小,使用名稱空間通常是消除衝突的好方法。
引進的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*/};
}
}
你的問題是關於JavaScript,但你用php和mysql標記? – 2013-05-06 13:09:31
@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
如果你運行使用Firebug的頁面會在下面的部分中看到一個錯誤(TypeError:$(...).dataTable不是函數): – 2013-05-06 13:15:36