首页 > 解决方案 > 为什么我的两个单独的脚本不能在我的 html 文档中串行运行?

问题描述

我正在尝试学习 JavaScript。我很新(本周开始)。我正在关注javascript.info提供的练习。

这是我在 Firefox 中打开的 html 代码:

<p>First, we have a prompt that incorrectly concatenates input strings.</p>
<script>
  let a = prompt("First number?", 1);
  let b = prompt("Second number?", 2);

  alert(a + b); // 12
</script>
<p>Now we correctly cast to integer, and then do addition.</p>
<script>
  let a = prompt("First number?", 1);
  let b = prompt("Second number?", 2);

  a = Number(a);
  b = Number(b);

  alert(a + b); // 3
</script>
<p>Well, that's all folks!</p>

出于某种原因,第一个脚本会运行,但第二个不会。我已经检查过了(https://www.quora.com/Can-I-call-two-JavaScript-files-in-one-HTML-fileHTML 中多个 <script> 标签的含义),看起来像他们应该按顺序运行。

标签: javascripthtml

解决方案


推荐阅读