jQuery冲突?
我正在一个JS文件中安装以下脚本,其中包含Wordpress博客的许多其他脚本。有人能告诉我一些工具的方向来检查冲突,因为当我把这个脚本放在站点构建中时它不起作用。(它本身工作得很好) 谢谢你jQuery冲突?,jquery,conflict,Jquery,Conflict,我正在一个JS文件中安装以下脚本,其中包含Wordpress博客的许多其他脚本。有人能告诉我一些工具的方向来检查冲突,因为当我把这个脚本放在站点构建中时它不起作用。(它本身工作得很好) 谢谢你 this.randomtip = function(){ var length = $("#message li").length; var ran = Math.floor(Math.random()*length) + 1; $("#message li:nth-child("
this.randomtip = function(){
var length = $("#message li").length;
var ran = Math.floor(Math.random()*length) + 1;
$("#message li:nth-child(" + ran + ")").show();
};
$(document).ready(function(){
randomtip();
});
试一试
我认为没有冲突。这只是一个范围问题 如果
randomtip
在范围内,则可以调用它,例如:
$(document).ready(function(){
function randomtip(){
var length = $("#message li").length;
var ran = Math.floor(Math.random()*length) + 1;
$("#message li:nth-child(" + ran + ")").show();
};
randomtip();
});
或
您是否在console..中看到任何错误?刚刚选中:ReferenceError:randomtip未定义[Break On This Error]randomtip();“这个。随机提示”,在什么范围内?甜菜根-如果我诚实的话,我不确定你的意思。脚本是从另一个源中删除的,因此我不确定。:-)好吧,你用这个限制了randomtip函数的范围,使它成为全局函数,它应该可以很好地工作,就像下面答案中建议的那样……这个列表中的第三个已经完成了这个技巧。非常感谢你。
$(document).ready(function(){
function randomtip(){
var length = $("#message li").length;
var ran = Math.floor(Math.random()*length) + 1;
$("#message li:nth-child(" + ran + ")").show();
};
randomtip();
});
var MYNAMESPACE = (function() {
var randomtip = function(){
var length = $("#message li").length;
var ran = Math.floor(Math.random()*length) + 1;
$("#message li:nth-child(" + ran + ")").show();
}
...
return {
randomtip: randomtip,
...
};
})();
$(document).ready(function(){
MYNAMESPACE.randomtip();
});