我如何才能不让 if 语句作为警报出现,而是以文本形式显示在我的页面上。谢谢
document.getElementById("button").onclick=function() {
if (document.getElementById("ask").value=="how tall are the pyramids") {
alert ("146.5 meters");
} else if (document.getElementById("ask").value == "how tall is the gateway arch") {
alert("630 feet");
} else if (document.getElementById("ask").value == "how tall is the empire state building") {
alert("6900 feet");
最佳答案
我可能误解了,但听起来您想在 html 元素中显示该值而不是警告该值。请参阅下面的示例
document.getElementById("button").onclick = function() {
var val = document.getElementById("ask").value;
var answer = "I don't know!";
if (val == "how tall are the pyramids")
answer = "146.5 meters";
else if (val == "how tall is the gateway arch")
answer = "630 feet";
else if (val == "how tall is the empire state building")
answer = "6900 feet";
document.getElementById("answer").innerHTML = answer;
}
并在 html 中添加一个答案元素,如下所示
<p id="answer"></p>
参见CodePen
关于javascript - 如何将 if 语句从警报更改为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32409678/