这是一个小问题,我开始学习JavaScript,目前正在测试其他if语句以及“提示”操作。但是此操作不会在此处执行,因为我无法给“鸡肉”赋予值
我到底做错了什么?
<html>
<head></head>
<body>
<script>
function state(){
var chicken = promt("insert value");
var alive = 1;
var dead = 2;
if (chicken == alive)
{
document.write("Chicken is Alive");
}
else if
{
document.write("Chicken is Dead");
}
}
</script>
</body>
</html>
答案 0 :(得分:1)
在下面的代码中添加了一些注释,希望对您有所帮助!
<html>
<head></head>
<body>
<script>
function state(){
var chicken = prompt("insert value");
var alive = 1;
var dead = 2;
//compare string to number? this chicken will never be alive!
//What if user types "maybe", we would compare 'maybe' == 1 //false
if (chicken == alive)
{
document.write("Chicken is Alive");
}
// you will always kill the chicken! you missed condition
else if (alive < 3)
{
document.write("Chicken is Dead");
}
}
//remember to call the function!
state()
</script>
</body>
</html>
答案 1 :(得分:0)
您没有在定义状态函数后调用它。除了markymarks答案外,在页面加载完成后加载用户javascript也是一个好习惯。
<body onload="myFunction()">