简体   繁体   English

为什么此javascript提示不起作用?

[英]Why isn't this javascript prompt working?

I am trying to make images of the specific candy pop up when you type in its name but only Ghirardelli works. 当您输入特定糖果的名称时,我试图弹出特定糖果的图像,但是只有Ghirardelli可以使用。

<p id="chocolate"></p>
<img src="hershey.jpg" id="pic">
<script>
var y = prompt("What would you like to see a picture of?");
if (y == "Hershey")
{
    document.getElementById("chocolate").innerHTML="Hershey";
}
if (y == "Reeses")
{
    document.getElementById("chocolate").innerHTML="Reeses";
    document.getElementById("pic").src="reeses.jpg";
}
if (y == "Ghiradelli")
{
    document.getElementById("chocolate").innerHTML="Ghiradelli";
    document.getElementById("pic").src="ghiradelli.jpg";
}
else
{
    document.getElementById("chocolate").innerHTML="Please choose one of these three things: <ul><li>Hershey</li><li>Reeses</li><li>Ghiradelli</li></ul>";
    document.getElementById("pic").src="x.svg";
}
</script>

That's because you only have one else in the whole thing. 那是因为你只能有一个else在整个事情。 The first two if statements are operating independently. 前两个if语句独立运行。 They work, but the final if is not true when they are, so the logic in the else overwrites anything the first two if 's did. 它们可以工作,但是最后一个if不成立时,则else的逻辑会覆盖前两个if所做的一切。

Just make the rest else if . 剩下的else if

var y = prompt("What would you like to see a picture of?");
if (y == "Hershey")
{
    document.getElementById("chocolate").innerHTML="Hershey";
}
else if (y == "Reeses")
{
    document.getElementById("chocolate").innerHTML="Reeses";
    document.getElementById("pic").src="reeses.jpg";
}
else if (y == "Ghiradelli")
{
    document.getElementById("chocolate").innerHTML="Ghiradelli";
    document.getElementById("pic").src="ghiradelli.jpg";
}
else
{
    document.getElementById("chocolate").innerHTML="Please choose one of these three things: <ul><li>Hershey</li><li>Reeses</li><li>Ghiradelli</li></ul>";
    document.getElementById("pic").src="x.svg";
}
  1. Use else if instead of multiple if. 使用else if而不是多个if。
  2. Try using return as the last statement of each if statement.(I am not sure about this point, checking) 尝试使用return作为每个if语句的最后一条语句。(对此我不确定,请检查)

Your if statements are not all being reached. 您的if陈述并没有全部达成。 A common solution to this problem is to clean up your code with either else if statements or using a switch statement. 解决此问题的常见方法是使用其他if语句或使用switch语句清理代码。 Also, I tried to abstract away from having to use too many document.getElementById statements 另外,我尝试从不必使用太多document.getElementById语句中抽象出来

<p id="chocolate"></p>

<img src="hershey.jpg" id="pic">

<script>

//sets variable y to the response in the promt
var y = prompt("What would you like to see a picture of?");

//sets the chocolate paragraph variable
var chocolate = document.getElementById("chocolate");

//sets the pic image variable
var pic = document.getElementById("pic");

//checks for different cases of y, use switch statements when there are too        many if statements
switch (y) {
case "Hershey":
    chocolate.innerHTML="Hershey";
    break;
case "Reeses":
    chocolate.innerHTML="Reeses";
    pic.src="reeses.jpg";
    break;
case "Ghiradelli":
    chocolate.innerHTML="Ghiradelli";
    pic.src="ghiradelli.jpg";
    break;
default:
    chocolate.innerHTML="Please choose one of these three things: <ul>  <li>Hershey</li><li>Reeses</li><li>Ghiradelli</li></ul>";
    pic.src="x.svg";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM