Javascript jQuery中的调用方法
我有这个方法:Javascript jQuery中的调用方法,javascript,jquery,html,fancybox,Javascript,Jquery,Html,Fancybox,我有这个方法: checkFancyBox=function(){ ldValue=LD(user,'admin')+LD(pass,'admin'); if(ldValue<2){ myHashMap[pass.concat(user)]=ldValue; $.fancybox({ content: "<span style='color:black'>Wrong username or password. </span>",
checkFancyBox=function(){
ldValue=LD(user,'admin')+LD(pass,'admin');
if(ldValue<2){
myHashMap[pass.concat(user)]=ldValue;
$.fancybox({
content: "<span style='color:black'>Wrong username or password. </span>",
showCloseButton: true,
transitionIn: "elastic"
});
$('#funny').show("");
return false;
}
};
它崩溃并立即刷新
当它被放置在$(document.ready)之外时。。。。它正在工作
但由于某些原因,它没有显示fancybox的内容
再一次,它会立即恢复活力
我应该将其放置在何处,以及如何使其显示内容并仅在
按下fancybox上的“关闭”按钮
谢谢大家! 这是一个吊装问题
checkFancyBox=function(){...};
};
将声明提升到函数顶部,但不提升初始化。所以当你有
$(document).ready(function() { ...
//checkFancyBox declaration is hoisted to here, and is undefined
checkFancyBox(); //calling an undefined variable is an error
checkFancyBox = function(){...}; //now checkFancyBox is initialized, but too late to prevent the error
};
将此声明放置在文档的外部。ready
处理程序会导致此声明和初始化在处理程序运行之前发生,因此checkFancyBox
有一个值,可以调用。如果您选择坚持原来声明函数的方式,请确保在函数前面放一个var
var checkFancyBox = function(){...};
但是请注意,函数可以这样简单地声明
function checkFancyBox(){
//...
}
另一方面,如果希望函数是document.ready处理程序的本地函数,则只需将声明移动到该处理程序的顶部:
$(document).ready(function() { ...
var checkFancyBox = function(){...};
//checkFancyBox declaration and initialization are both accomplished,
//and the function is safe to call
checkFancyBox();
};
@Itzik984-将函数放在document.ready处理程序之外绝对是声明函数的正确方法,正如您所看到的,没有发生错误。恐怕上面没有足够的代码来解释它为什么不工作。我只需要正确声明函数,收集所有相关代码,然后发布一个新问题。我确信有一些fancybox专家可以帮助你。我将它放在doc.ready之外,它可以工作,但会立即刷新,并且不会显示所需的消息。我是不是把fancybox弄错了?如何阻止它提神?@Itzik984-恐怕我不是fancybox专家。正如我所说,收集checkFancyBox函数的所有相关代码,然后发布一个新问题。
$(document).ready(function() { ...
var checkFancyBox = function(){...};
//checkFancyBox declaration and initialization are both accomplished,
//and the function is safe to call
checkFancyBox();
};