【问题标题】:Javascript Prompt() only working inline and not in external fileJavascript Prompt() 只能内联工作,不能在外部文件中工作
【发布时间】:2017-04-18 09:50:17
【问题描述】:

我有一个 prompt() 变量,它仅在它位于 html 文件中的 <script> 标记内时才有效,但要遵守一些我不能这样做的约束。相反,我将它放在.js 文件中,但是当我刷新页面时它不会提示我。文件已加载,但没有任何反应。任何想法为什么会发生这种情况?

更新代码

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>

    <link rel="stylesheet" type="text/css" href="index.css">

    <script type="text/javascript" href="index.js"></script>
</head>
<body>
    <div class="display">

    </div>
</body>
</html>

【问题讨论】:

  • 您可以将您的代码添加到问题中吗? (见minimal reproducible example)。
  • 您是指&lt;style&gt; 标签还是&lt;script&gt; 标签? prompt() 是 javascript,而不是 css。
  • @shaochuancs 绝对是&lt;script&gt;哈哈。这里真的很晚了。感谢您了解这一点。
  • @evolutionxbox 我会添加代码
  • &lt;script&gt;中使用src,而不是href

标签: javascript html css prompt


【解决方案1】:
<script type="text/javascript" href="index.js"></script>

您要查找的属性是src 而不是href

使用a validator

【讨论】:

  • 我真的不应该在凌晨 4 点编码。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多