js中if else的优化代码
if(i==0){$("#preview.jqzoomimg").attr("src","images/Medical_1/zsq-01s.jpg");$("#previ...
if(i==0) {
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==1){
$("#preview .jqzoom img").attr("src","images/Medical_1/zhushezhen-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zhushezhen-01b.jpg");
}else if(i==2){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}
else if(i==3){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==4){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==5){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==6){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}
请问怎么用数组 或者其他方法优化代码 这样写太臃肿 展开
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==1){
$("#preview .jqzoom img").attr("src","images/Medical_1/zhushezhen-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zhushezhen-01b.jpg");
}else if(i==2){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}
else if(i==3){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==4){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==5){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}else if(i==6){
$("#preview .jqzoom img").attr("src","images/Medical_1/zsq-01s.jpg");
$("#preview .jqzoom img").attr("jqimg","images/Medical_1/zsq-01b.jpg");
}
请问怎么用数组 或者其他方法优化代码 这样写太臃肿 展开
2个回答
展开全部
var srcArray = ["zsq-01s.jpg", "zhushezhen-01s.jpg", "zsq-01s.jpg", "zsq-01s.jpg", "zsq-01s.jpg", "zsq-01s.jpg", "zsq-01s.jpg"],
imgArray = ["zsq-01b.jpg", "zhushezhen-01b.jpg", "zsq-01b.jpg", "zsq-01b.jpg", "zsq-01b.jpg", "zsq-01b.jpg", "zsq-01b.jpg"];
$("#preview .jqzoom img").attr("src", "images/Medical_1/" + srcArray[i])
.attr("jqimg", "images/Medical_1/" + imgArray[i]);
可以随意修改2个数组里的内容。
如果只有i==2的时候不同,就更简单了,拼地址的时候用i==2 ? "zhushezhen-01s.jpg" : "zsq-01s.jpg" 就行了。
2017-08-29
展开全部
厉害了 用switch呢 好像也差不多
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询