You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何结合Boolean布尔值与if else语句并在HTML中输出运行结果

代码修复说明

错误原因

代码的核心语法错误是误用了其他强类型编程语言的变量声明规则:JavaScript中不存在boolean这个声明关键字,声明变量需要使用var、let或者const。

修复后完整代码

<body>
  <p id="demo"></p>
  <script>
    // 布尔值可根据实际天气场景修改为true/false
    const thunderstorm = true;
    if (thunderstorm) {
      document.getElementById("demo").innerHTML = "Stay at home";
    } else {
      document.getElementById("demo").innerHTML = "Happy Journey";
    }
  </script>
</body>

功能验证

  • 当thunderstorm赋值为true时,页面显示「Stay at home」
  • 当thunderstorm赋值为false时,页面显示「Happy Journey」

内容的提问来源于stack exchange,提问作者user17561422

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 08:06:04