Javascript jQuery淡出回调函数-为什么本地函数会赢';当全局函数将工作时,它不工作吗?
我得到了一个简单的Javascript jQuery淡出回调函数-为什么本地函数会赢';当全局函数将工作时,它不工作吗?,javascript,jquery,function,callback,scope,Javascript,Jquery,Function,Callback,Scope,我得到了一个简单的twitter引导程序消息框架,上面有一些jQuery效果 首先,我淡出黑板,一旦它淡出完毕,我正在写我想写的东西,然后他们再次淡入 我有两个版本: 首先,使用一个本地定义函数: function postMessage(message, context) { if(typeof context === 'undefined') context = "info"; var newClass = "alert alert-" + context;
twitter引导程序
消息框架,上面有一些jQuery效果
首先,我淡出黑板,一旦它淡出完毕,我正在写我想写的东西,然后他们再次淡入
我有两个版本:
首先,使用一个本地定义函数:
function postMessage(message, context) {
if(typeof context === 'undefined')
context = "info";
var newClass = "alert alert-" + context;
var messageBoard = $("#messageBoard");
// Fade out
messageBoard.fadeOut('slow', postFadeOut);
var postFadeOut = function() {
alert("XX");
// $(this).attr('class', newClass);
// $(this).html(message);
// // Fade in again
// $(this).fadeIn('slow');
}
}
它不会触发警报(“XX”)
,但:
function postMessage(message, context) {
if(typeof context === 'undefined')
context = "info";
var newClass = "alert alert-" + context;
var messageBoard = $("#messageBoard");
// Fade out
messageBoard.fadeOut('slow', postFadeOut);
}
function postFadeOut() {
alert("XX");
// $(this).attr('class', newClass);
// $(this).html(message);
// // Fade in again
// $(this).fadeIn('slow');
}
将触发。为什么?在调用
留言板之前,尝试声明postfeadeout
变量。fadeOut('slow',postfeadeout)
函数postMessage(消息、上下文){
如果(上下文类型==='undefined'){
context=“info”;
};
var newClass=“警报-”+上下文;
var留言板=$(“#留言板”);
var postFadeOut=函数(){
$(this.attr('class',newClass));
$(this.html(message);
//再次淡入
$(this.fadeIn('slow');
}
//淡出
留言板淡出(“慢”,淡出后);
}
postMessage(“消息”)
留言板
这是一种效果
在JavaScript中,变量被提升。这意味着在声明它们的范围内,它们可以在任何代码行中使用,即使在该行之后声明。但是,它们的值或初始化是按照编写代码的顺序进行的。例如:
警报(a);
var a=‘某些值’;
警报(a)在调用.fadeOut()
之前,code>尝试声明var postfeadeout
?当if
语句中的=
运算符时,if
语句是否应该有括号{}
?可以在问题中包含html
?回答得很好!谢谢