为什么 JavaScript toLowerCase 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6759733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:45:44  来源:igfitidea点击:

why doesn't JavaScript toLowerCase work?

javascriptjquery

提问by Leahcim

In this fiddle http://jsfiddle.net/CBxbT/29/, if you click the black box, a random color is selected, and then after its id is stripped down and changed to lowercase, the random color's id is compared to whatever color you "guess" by clicking on one of the three boxes.

在这个小提琴http://jsfiddle.net/CBxbT/29/ 中,如果您单击黑框,则会选择随机颜色,然后将其 id 剥离并更改为小写后,将随机颜色的 id 与任何颜色进行比较通过单击三个框之一为您“猜测”的颜色。

The fiddle works, but the same concept (i.e same code but different element names) is not working on my live site. One my live site, I get an alert for the variable guess, but then I don't get anymore alerts, whereas in the fiddle, it keeps going and gives me an alert after ranis changed to lower case.

小提琴有效,但相同的概念(即相同的代码但不同的元素名称)在我的实时站点上不起作用。在我的实时站点中,我收到了关于变量的警报guess,但随后我不再收到警报,而在小提琴中,它继续运行并在ran更改为小写后给我一个警报。

Is there a problem with the way I did that might be causing the problem on my live site.

我的做法是否有问题,可能会导致我的实时网站出现问题。

ran = ran.toLowerCase();

code from fiddle

来自小提琴的代码

$("#red, #blue, #green").click(function(e) { 
     guess = $(this).attr('id'); 
     alert(guess);
     ran = ran.toLowerCase(); //works here but not on my live site
    alert(ran);
    if(ran.charAt(1) === 'f'){
        ran = ran.slice(3);
    } else
        ran = ran.slice(1);
        alert(ran);

     if (guess === ran) { 
         $('#results').fadeOut(1000);
              } else { 
                  $('#wrong').fadeOut(1000); 
                  }
 }); 

My live site my actual site

我的实时站点我的实际站点

Click the start button. It will show an audio player that is randomly selected.

单击开始按钮。它将显示随机选择的音频播放器。

Click one of the spelled-out numbers from the bottom row of numbers. That is your "guess"

单击最下面一行数字中拼写出来的数字之一。那是你的“猜测”

It should, according to the code, give you an alert of the randomly selected audio player number, but it doesn't.

根据代码,它应该会提醒您随机选择的音频播放器编号,但事实并非如此。

relevant code from live site

来自实时站点的相关代码

$("#one, #two, #three, #four, #five, #six, #seven, #eight, #nine, #ten, #eleven, #twelve, #thirteen, #fourteen, #fifteen, #sixteen").click(function(e) { 
     guess = $(this).attr('id'); 
     alert(guess);
     ran = ran.toLowerCase(); 
     alert(ran);
     if(ran.charAt(1) === 'f'){
        ran = ran.slice(3);
    } else
        ran = ran.slice(1);
     alert(ran);
     if (guess === ran) { 
         $('#right').fadeIn(1000);
              } else { 
                  $('#wrong').fadeIn(1000); 
                  }
 }); 
}

采纳答案by JJJ

If you look at the JavaScript error console, you'll see this:

如果您查看 JavaScript 错误控制台,您会看到:

ran is undefined

I suggest you start using Firebug or something similar so that you'll see the error console while you're developing.

我建议你开始使用 Firebug 或类似的东西,这样你就可以在开发时看到错误控制台。

On your live site the event for clicking on the "start" button has this line:

在您的实时站点上,单击“开始”按钮的事件包含以下行:

var ran = getRandom(myArray, true);

The varkeyword creates a local variable called ran, so you're creating a new variable and not initializing the global one. Remove varfrom that line and it should work.

var关键字创建了一个名为 的局部变量ran,因此您正在创建一个新变量而不是初始化全局变量。var从该行中删除,它应该可以工作。