我试图使用if,else if,else语句,但是一旦我添加了else if,它似乎无法工作。如果我只使用if语句,一切正常,但是一旦我添加了else if,否则它就会停止工作。
var portAnswer = "";
var strQuestion = "Did you make it to port on time?";
var strDefault = "Yes/No";
portAnswer = prompt(strQuestion, strDefault);
var strMessage;
$(document).ready(function() {
if (portAnswer == "yes") {
strMessage = "Arghh, you are awarded extra doubloons!";
} else if (portAnswer == "no") {
strMessage = "Arggh, walk that there plank!");
} else {
strMessage = "Arggh, I don't understand!");
}
var strOutput = document.getElementById("promptParagraph");
strOutput.textContent = strMessage;
});
答案 0 :(得分:1)
删除在JavaScript控制台中显示为错误的额外结束括号:
if (portAnswer == "yes") {
strMessage = "Arghh, you are awarded extra doubloons!";
} else if (portAnswer == "no") {
strMessage = "Arggh, walk that there plank!";
} else {
strMessage = "Arggh, I don't understand!";
}