javascript - 如何将 if 语句从警报更改为文本

标签 javascript input

我如何才能不让 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/

相关文章:

css - 定位所有浏览器的表单输入字段

javascript - 如何访问 angularjs 中动态创建的字段?

Javascript:根据输入匹配数据列表选项

javascript - 获取输入文本框的值并将其添加到文本区域

javascript - 如何使 JavaScript 函数使用更少的内存(后台图像旋转)

javascript - 在javascript中的一系列函数之后返回

javascript - 用 js 函数 window.history.back() 添加一个参数

javascript - 如何在 xAxis 中获取带有日期的类别?

javascript - MongoDB $geoNear 距离返回值精度下降

javascript - 显示需要用户名和密码的消息是什么?