代码之家  ›  专栏  ›  技术社区  ›  adardesign

为什么我的开关块(打开$(e.target).attr(“类”)不工作?

  •  0
  • adardesign  · 技术社区  · 16 年前

    我正在试图弄明白为什么这个代码不起作用。。

    请让我知道这里出了什么问题:

    $(document).ready(function() {
      var img = $("img");
    
    $("span").click(function(e){
      var targetClicked = $(e.target).attr('class');
      //the alert works fine
      alert(targetClicked)
    
    switch(targetClicked){
    // i deleted the rest of the cases
        case d:img.stop(false,true);
      break;
        case e:img.slideDown().animate({"width":200, height:200, opacity:0.4,});
      break; 
      //nothings works here as well
        case f:alert("hi");
      break;
     }
     });         
    });
    
    1 回复  |  直到 16 年前
        1
  •  3
  •   Annabelle    16 年前

    是什么 d e switch 陈述 case 条件您编写代码的方式是现在,它们被视为变量,您的代码可能会出现“d”是未定义的错误。

    "d" "e" ,则需要将类名用作字符串:

    switch (targetClicked) {
      case "d":
        //...
        break;
      case "e":
        // ...
        break;
    }