Javascript if else条件运行时失败
Javascript if else condition run time failure
我今天已经开始使用javascript了。尝试使用最基本的If Else循环,但被卡住了。
<>之前Var input = prompt("输入你的名字");//变量存储用户输入的值Var outout = tostring(输入);//将输入值更改为字符串数据类型并存储在var output中Alert (output);//应该显示它没有显示的值if(output == "Tiger"){alert("这是危险的");}其他的{alert("一切正常");}//我只得到一个空白页之前如果省略var output = tostring(input)行,并尝试显示带有输入值的警告框,则会得到警告框。但在那之后,我只能得到一张白纸。If Else循环根本不起作用。我用的是notepad++。在Dreamweaver中也检查过。没有编译错误。我做错了什么?很抱歉问了这么一个基本的问题,谢谢你的回复。
问候,TD
你的台词
tostring(input);
toString(input);
toString()
方法有一个大写S
同样,你的输出变量被称为"outout"。不知道是不是打错了…
不仅如此,您的Else
也应该有一个小的e
。所有JavaScript关键字都区分大小写
您不必将提示的结果转换为字符串,它已经是字符串了。实际上应该是
input.toString()
如果Else
是小写的,那么正确的是else
。
你可以这样写
var input = prompt("Type your name");
if (input == "Tiger")
{
alert("Wow, you are a Tiger!");
}
else
{
alert("Hi " + input);
}
注意,如果您键入tiger
(小写),您将在else
上结束。如果你想比较一个不区分大小写的字符串,你可以这样做:
if (input.toLowerCase() == "tiger")
那么即使tIgEr
也可以工作
你的代码有以下问题:
var input = prompt("type your name");
var outout = tostring(input);
// Typo: outout should be output
// tostring() is not a function as JavaScript is case-sensitive
// I think you want toString(), however in this context
// it is the same as calling window.toString() which is going to
// return an object of some sort. I think you mean to call
// input.toString() which, if input were not already a string
// (and it is) would return a string representation of input.
alert(output);
// displays window.toString() as expected.
if(output == "Tiger")
{alert("It is dangerous");
}
Else // JavaScript is case-sensitive: you need to use "else" not "Else"
{alert("all is well");
}//I only get a blank page
我猜你想要的是这个:
var input = prompt("type your name");
alert(input);
if (input === "Tiger") {
alert("It is dangerous");
} else {
alert("all is well");
}
相关文章:
- 使用压缩的JavaScript文件(不是运行时压缩)
- 如何在运行时在angular 2中加载外部js脚本
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- http.listen()在运行时接受终端命令
- 自定义运行时Can'在谷歌应用引擎中看不到我的自定义日志
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- JavaScript运行时是如何工作的
- 在运行时创建元素时移到一边时出错
- 如何在运行时在HTML5画布中绘制正方形
- 如何在运行时使用javascript隐藏图像
- 只有从Node运行时,Rsync才会失败,退出代码为23
- 在Microsoft Surface(Excel VBA)上运行GetJSObject时失败
- 如何在node中编写事件发射器.js让你创建一个函数“myFunction”,然后在运行时调用成功或失败
- Jasmine在同一测试运行两次时失败
- Javascript if else条件运行时失败
- Karma/jasmine Angular指令submit click单元测试在一堆运行时失败,但不是单独运行
- 按位操作容易在运行时typescript失败
- perl-cgi脚本可以't发布到站点-独立工作,但在apache下运行时失败
- ActiveXObject在从.NET运行时工作,但在发布后失败