javascript 的回调函数,带参数和不带参数的使用
(2014-04-24 16:14:48)
标签:
回调函数回调函数带参数回调函数带参和不带参it |
分类: JS |
越不想学什么,越就被什么所折磨。看看javascript回调吧。 直接上码吧。。
本文转载至:http://blog.csdn.net/yippan/article/details/6642309
-
-
<</span>html>
-
-
<</span>head> -
-
<</span>title>回调函数(callback)</</span>title> -
<</span>script language="javascript" type="text/javascript"> -
-
function
a(callback){ -
alert("我来自父函数a"); -
-
callback(); -
-
}
-
function
b(){ -
alert("我来自子(回调)函数 b"); -
-
}
-
-
function
c(){ -
alert("我来自子(回调)函数 c"); -
} -
-
-
function test(){ -
-
a(b); -
-
a(c); -
} -
-
//--------------------下面2个函数简单演示了从父函数传递给回调函数----------
-
-
//Callback
就是一个参数名 这个参数代表的是一个函数 暂且不要管这个函数有没有参数 -
//
。。。 应该还有其他写法吧? -
function
e(m,n,Callback){ -
var d = m+n; -
alert("一个从父函数e 产生的参数将要被传递给回调函数 ,这个参数是:"+d); -
-
//这里才写你想调用的函数---参数要正确 -
Callback(d); -
//-----------
-
}//
| -
//
| -
function
callback(data){ -
-
alert("我是回调函数,我的名字叫:callback ,我接收到来自父函数的参数,参数是:"+data); -
-
}
-
-
-
</</span>script>
-
</</span>head>
-
-
<</span>body>
-
<</span>h2>a CallBack </</span>h2>is a function that is passed as an argument to another function and is executed after its parent function has completed; -
<</span>hr /> -
<</span>a href="#" onclick="javascript:test();">不传递参数的Callback</</span>a><</span>br /> -
<</span>br /> -
<</span>br /> -
<</span>br /> -
<</span>a href="#" onclick="javascript:e(7,8,callback);">传递参数的Callback</</span>a> -
<</span>hr /> -
</</span>body> -
-
</</span>html>