【问题标题】:Switch variable issue切换变量问题
【发布时间】:2016-04-21 23:18:23
【问题描述】:

在开发所谓的“内容滑块”时,我编写了这段代码:

var dayy = 1;

$(".buttonSix").click(function() {
  dayy--;
  console.log(dayy);
});

$(".buttonSeven").click(function() {
  dayy++;
  console.log(dayy);
});


switch (dayy)
{
  case 0:
    var dayy = 1;
    break;
  case 1:
    break;
  case 2:
    $(".fist").fadeOut();
    $(".second").fadeOut();
    $(".third").fadeIn();
    $(".fourth").fadeIn();
    break;
  case 3:
    $(".third").fadeOut();
    $(".fourth").fadeOut();
    $(".fith").fadeIn();
    $(".sicth").fadeIn();
    break;
  case 4:
    $(".fith").fadeOut();
    $(".sicth").fadeOut();
    $(".seventh").fadeIn();
    $(".eight").fadeIn();
    break;
  case 5:
    $(".seventh").fadeOut();
    $(".eight").fadeOut();
    $(".nineth").fadeIn();
    $(".tenth").fadeIn();
    break;
  case 6:
    $(".nineth").fadeOut();
    $(".tenth").fadeOut();
    $(".eleventh").fadeIn();
    $(".twelve").fadeIn();
    break;
  case 7:
    $(".eleventh").fadeOut();
    $(".twelve").fadeOut();
    $(".thirteenth").fadeIn();
    $(".fourteenth").fadeIn();
    break;
  default:
    $(".thirteenth").fadeOut();
    $(".fourteenth").fadeOut();
    $(".first").fadeIn();
    $(".second").fadeIn();
    var dayy = 1;
    break;
}

所有的 html div 都没有显示,除了第一个和第二个可见。 增量正在记录并工作,但开关不工作。

没有记录控制台错误

感谢所有帮助和 cmets, 提前非常感谢您

【问题讨论】:

    标签: javascript jquery slider switch-statement


    【解决方案1】:

    你的开关不是点击后调用的,应该是:

    var dayy = 1;
    
        $(".buttonSix").click(function() {
          switchDayy(--dayy);
        });
    
        $(".buttonSeven").click(function() {
          switchDayy(++dayy)
        });
    
    function switchDayy(dayy){
        switch (dayy) {
          case 0:
            var dayy = 1;
            break;
          case 1:
            break;
          case 2:
            $(".fist").fadeOut();
            $(".second").fadeOut();
            $(".third").fadeIn();
            $(".fourth").fadeIn();
            break;
          case 3:
            $(".third").fadeOut();
            $(".fourth").fadeOut();
            $(".fith").fadeIn();
            $(".sicth").fadeIn();
            break;
          case 4:
            $(".fith").fadeOut();
            $(".sicth").fadeOut();
            $(".seventh").fadeIn();
            $(".eight").fadeIn();
            break;
          case 5:
            $(".seventh").fadeOut();
            $(".eight").fadeOut();
            $(".nineth").fadeIn();
            $(".tenth").fadeIn();
            break;
          case 6:
            $(".nineth").fadeOut();
            $(".tenth").fadeOut();
            $(".eleventh").fadeIn();
            $(".twelve").fadeIn();
            break;
          case 7:
            $(".eleventh").fadeOut();
            $(".twelve").fadeOut();
            $(".thirteenth").fadeIn();
            $(".fourteenth").fadeIn();
            break;
          default:
            $(".thirteenth").fadeOut();
            $(".fourteenth").fadeOut();
            $(".first").fadeIn();
            $(".second").fadeIn();
            var dayy = 1;
            break;
    
        }
    }
    

    【讨论】:

    • 这看起来不太对劲,这段代码中var dayy = 1;太多了。
    • @Musa 去掉了 var,只留下 dayy = 1 因为不需要 var,但其余代码确实完成了工作:)谢谢!
    • @Musa 是的,它更像是伪代码而不是真实代码,因为点击必须在代码的单独部分中的文档就绪函数中。只有一个全局变量 dayy 就足够了,然后让它在 switchDayy() 上有一个参数是没有用的,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多