jQuery插件扩展实例【添加回调函数】插件扩展实例【添加回调函数】
本文实例讲述了jQuery插件扩展的方法。分享给大家供大家参考,具体如下:
<script language="javascript" type="text/javascript">
function doSomething(callback) {
// …
// Call the callback
callback('stuff', 'goes', 'here'); // 给callback赋值,callback是个函数变量
}
function foo1(a, b, c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here
var foo2 = function(a,b,c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函数将使用callback函数中的数据 stuff goes here
doSomething(function(a,b,c){
alert(a + " " + b + " " + c); // function函数将使用callback函数中的数据 stuff goes here
});
</script>
callback这个参数必须是函数才有效。才能起到回调的作用。
<script language="javascript" type="text/javascript">
function doSomething(callback) {
// …
// Call the callback
if(typeof callback === 'function'){
callback('stuff', 'goes', 'here'); // 给callback赋值,callback是个函数变量
}else{
alert('jb51.net');
}
}
function foo1(a, b, c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here
var foo2 = function(a,b,c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函数将使用callback函数中的数据 stuff goes here
doSomething(function(a,b,c){
alert(a + " " + b + " " + c); // function函数将使用callback函数中的数据 stuff goes here
});
var foo3 = 'a';
doSomething(foo3);
</script>
foo3不是函数的时候,弹出jb51.net
jQuery实例实例
原函数
$.fn.citySelect=function(settings)
添加回调
$.fn.citySelect=function(settings, changeHandle) // 添加回调函数changeHandle
给回调函数赋值
//选项变动赋值事件
var selectChange = function (areaType) {
if(typeof changeHandle === 'function'){ // 判断callback是否是函数
var prov_id = prov_obj.get(0).selectedIndex;
var city_id = city_obj.get(0).selectedIndex;
评论0
最新资源