为了账号安全,请及时绑定邮箱和手机立即绑定

我的代码有两段 if 判断是重复的,请问怎么写能简化代码?

我的代码有两段 if 判断是重复的,请问怎么写能简化代码?

阿尔滨必胜 2016-10-31 22:36:07
if  (num==00){}  if (num==01){}  ..................这块有两段 if 判断是重复的,能不能给封装起来?或者怎么写能简化代码?/*向右按钮点击切换----开始*/     var prev_num = null;     var ibm_carousel_next = document.getElementsByClassName('ibm-carousel-next')[0];     ibm_carousel_next.onclick = function() {         var slick_dots = document.getElementsByClassName('slick-dots')[0];         var slick_dots_li = slick_dots.getElementsByTagName('li');         for (var i = 0; i < slick_dots_li.length; i++) {             if (slick_dots_li[i].getAttribute('class') == 'slick-active') {                 prev_num = slick_dots_li[i].id;                 console.log(prev_num);                 var myString = prev_num;                 var num = myString.substring(11, 13);                 console.log(num);                 if (num == 00) {                     var h3_description = document.getElementById('h3_description');                     var p_description_1 = document.getElementById('p_description_1');                     var p_description_2 = document.getElementById('p_description_2');                     h3_description.innerHTML='';                     p_description_1.innerHTML='Watson is a cognitive system that understands, reasons and learns. Marchesa is an iconic high fashion brand that inspires, empowers and creates. See how they worked together to outthink the limits of fashion.';                     p_description_2.innerHTML='';                     var thumbnail = document.getElementsByClassName('thumbnail');                     for (var i = 0; i < thumbnail.length; i++) {                         thumbnail[0].src = "./images/thumbnail_0.jpg";                         thumbnail[1].src = "./images/thumbnail_1.jpg";                         thumbnail[2].src = "./images/thumbnail_2.jpg";                     };                 }                 if (num == 01) {                     var h3_description = document.getElementById('h3_description');                     var p_description_1 = document.getElementById('p_description_1');                     var p_description_2 = document.getElementById('p_description_2');                     h3_description.innerHTML='Man-and-Machine Mix-and-Match';                     p_description_1.innerHTML='How can designers keep materials exciting? IBM Watson developer partner <a target="_blank" href="http://www.inno-360.com/">Inno360</a> used cognitive APIs to help IBM material scientist Victoria Piunova expand her expertise. Watson read over 40,000 sources and helped Marchesa discover materials to bring their fabrics to life.';                     p_description_2.innerHTML='';                     var thumbnail = document.getElementsByClassName('thumbnail');                     for (var i = 0; i < thumbnail.length; i++) {                         thumbnail[0].src = "./images/thumbnail_1.jpg";                         thumbnail[1].src = "./images/thumbnail_2.jpg";                         thumbnail[2].src = "./images/thumbnail_3.jpg";                     };                 }             }         }     };     /*向右按钮点击切换----结束*/     /*向左按钮点击切换----开始*/     var next_num = null;     var ibm_carousel_prev = document.getElementsByClassName('ibm-carousel-prev')[0];     ibm_carousel_prev.onclick = function() {         var slick_dots = document.getElementsByClassName('slick-dots')[0];         var slick_dots_li = slick_dots.getElementsByTagName('li');         for (var i = 0; i < slick_dots_li.length; i++) {             if (slick_dots_li[i].getAttribute('class') == 'slick-active') {                 next_num = slick_dots_li[i].id;                 console.log(next_num);                 var myString = next_num;                 var num = myString.substring(11, 13);                 console.log(num);                 if (num == 00) {                     var h3_description = document.getElementById('h3_description');                     var p_description_1 = document.getElementById('p_description_1');                     var p_description_2 = document.getElementById('p_description_2');                     h3_description.innerHTML='';                     p_description_1.innerHTML='Watson is a cognitive system that understands, reasons and learns. Marchesa is an iconic high fashion brand that inspires, empowers and creates. See how they worked together to outthink the limits of fashion.';                     p_description_2.innerHTML='';                     var thumbnail = document.getElementsByClassName('thumbnail');                     for (var i = 0; i < thumbnail.length; i++) {                         thumbnail[0].src = "./images/thumbnail_0.jpg";                         thumbnail[1].src = "./images/thumbnail_1.jpg";                         thumbnail[2].src = "./images/thumbnail_2.jpg";                     };                 }                 if (num == 01) {                     var h3_description = document.getElementById('h3_description');                     var p_description_1 = document.getElementById('p_description_1');                     var p_description_2 = document.getElementById('p_description_2');                     h3_description.innerHTML='Man-and-Machine Mix-and-Match';                     p_description_1.innerHTML='How can designers keep materials exciting? IBM Watson developer partner <a target="_blank" href="http://www.inno-360.com/">Inno360</a> used cognitive APIs to help IBM material scientist Victoria Piunova expand her expertise. Watson read over 40,000 sources and helped Marchesa discover materials to bring their fabrics to life.';                     p_description_2.innerHTML='';                     var thumbnail = document.getElementsByClassName('thumbnail');                     for (var i = 0; i < thumbnail.length; i++) {                         thumbnail[0].src = "./images/thumbnail_1.jpg";                         thumbnail[1].src = "./images/thumbnail_2.jpg";                         thumbnail[2].src = "./images/thumbnail_3.jpg";                     };                 }             }         }     }
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 1791 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信