Js函数参数能不能引用传递
var hotel_index = 0; var dining_index = 0; var rides_index = 0; var get_there_index = 0; $('#hotel_right_arrow').click(function(){ if (hotel_index < len_hotels - 3){ hotel_index += 1; display_or_not($(".hotel_box"), hotel_index); } if (hotel_index == len_hotels - 3){ $('#hotel_right_arrow').css({'opacity': '0.5'}); } $('#hotel_left_arrow').css({'opacity': '1.0'}); }); $('#hotel_left_arrow').click(function(){ if (hotel_index > 0){ hotel_index -= 1; display_or_not($(".hotel_box"), hotel_index); } if (hotel_index == 0){ $('#hotel_left_arrow').css({'opacity': '0.5'}); } $('#hotel_right_arrow').css({'opacity': '1.0'}); }); //dining_right_arrow $('#dining_right_arrow').click(function(){ if (dining_index < len_dining - 3){ dining_index += 1; display_or_not($(".dining_box"), dining_index); } if (dining_index == len_dining - 3){ $('#dining_right_arrow').css({'opacity': '0.5'}); } $('#dining_left_arrow').css({'opacity': '1.0'}); }); $('#dining_left_arrow').click(function(){ if (dining_index > 0){ dining_index -= 1; display_or_not($(".dining_box"), dining_index); } if (dining_index == 0){ $('#dining_left_arrow').css({'opacity': '0.5'}); } $('#dining_right_arrow').css({'opacity': '1.0'}); }); //rides_right_arrow $('#rides_right_arrow').click(function(){ if (rides_index < len_rides- 3){ rides_index += 1; display_or_not($(".rides_box"), rides_index); } if (rides_index == len_rides - 3){ $('#rides_right_arrow').css({'opacity': '0.5'}); } $('#rides_left_arrow').css({'opacity': '1.0'}); }); $('#rides_left_arrow').click(function(){ if (rides_index > 0){ rides_index -= 1; display_or_not($(".rides_box"), rides_index); } if (rides_index == 0){ $('#rides_left_arrow').css({'opacity': '0.5'}); } $('#rides_right_arrow').css({'opacity': '1.0'}); }); //get there $('#get_there_right_arrow').click(function(){ if (get_there_index < len_get_there- 3){ get_there_index += 1; display_or_not($(".get_there_box"), get_there_index); } if (get_there_index == len_get_there - 3){ $('#get_there_right_arrow').css({'opacity': '0.5'}); } $('#get_there_left_arrow').css({'opacity': '1.0'}); }); $('#get_there_left_arrow').click(function(){ if (get_there_index > 0){ get_there_index -= 1; display_or_not($(".get_there_box"), get_there_index); } if (get_there_index == 0){ $('#get_there_left_arrow').css({'opacity': '0.5'}); } $('#get_there_right_arrow').css({'opacity': '1.0'}); });
上面的代码怎么写成函数, 写成如下的形式啊,关键这个index变量怎么引用传递,我希望的效果是函数内的变化要影响到外面的各个index值
function right_arrow_clicked(a,b,c,d,index){.....} $('#hotel_right_arrow').click(function(){ right_arrow_clicked(a,b,c,d,index); } $('#dining_right_arrow').click(function(){ right_arrow_clicked(a,b,c,d,index); } $('#rides_right_arrow').click(function(){ right_arrow_clicked(a,b,c,d,index); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本类型不能引用传递,可以做成object就可以引用传递了
var indexobj = {index:0}
xxxxfn(indexobj)
xxxxfn(indexobj)
使用全局变量,在函数内部用this调用