0

我正在开发的网站上运行 2 个脚本,其中一个与另一个冲突

第二个只有在第一个丢失时才有效。所以我不知道问题是什么。

你可以看到这个链接。

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

在此页面中,预览标题位于第 3 部分,在该部分中,如果您单击任何标题灯箱将出现,但左侧菜单下拉脚本不起作用并且与灯箱脚本冲突。

4

1 回答 1

1

根据脚本的大小,使用命名空间通常是消除冲突的好方法。

两种最常见的介绍方式是:

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*/};
  }
}
于 2013-05-06T13:35:02.153 回答