如何防止JavaScript文件与其他文件冲突?
我有两个脚本在我正在开发的站点上运行,一个脚本和另一个脚本冲突 第二个只有在第一个丢失的情况下才起作用。所以我不知道问题出在哪里 你可以看到这个链接如何防止JavaScript文件与其他文件冲突?,javascript,Javascript,我有两个脚本在我正在开发的站点上运行,一个脚本和另一个脚本冲突 第二个只有在第一个丢失的情况下才起作用。所以我不知道问题出在哪里 你可以看到这个链接 在第三部分有预览标题的页面中,如果您单击任何标题灯箱,将显示该部分,但左侧菜单下拉脚本不起作用,并且与灯箱脚本冲突。根据脚本的大小,使用名称空间通常是消除冲突的好方法 最常见的两种介绍方式是: var ns = ns || {}; ns.ondocready = function(){/*your declaration here*/}; ns.
在第三部分有预览标题的页面中,如果您单击任何标题灯箱,将显示该部分,但左侧菜单下拉脚本不起作用,并且与灯箱脚本冲突。根据脚本的大小,使用名称空间通常是消除冲突的好方法 最常见的两种介绍方式是:
var ns = ns || {};
ns.ondocready = function(){/*your declaration here*/};
ns.onsomeotherfunct = function(arg){/*your declaration here*/};
或
然后调用命名对象中的函数(例如名称空间)
唯一需要注意的是,当使用“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对其进行了标记???@Jhansi如果您使用Firebug运行页面,您可能会在以下部分看到错误(TypeError:$(…)。dataTable不是函数:$(document).ready(function(){$('.dataTable')。dataTable();});
ns.ondocready();
othernamespace.someotherfunction('test');
othernamespace.publicvar = 'available to functions within namespace by referencing with "this" keyword';
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*/};
}
}