Javascript onload 不适用于我的随机单词生成器

标签 javascript

js 中的 onload 对我不起作用。我试图让它随机生成刽子手游戏的电影,但是,它不想加载。我也尝试在 CodePen 中运行它,所以它与链接无关。任何建议都会很棒。

var movieTitles = [
  "halloween",
  "suspiria",
  "audition",
  "hereditary",
  "the beyond",
  "the evil dead",
  "the blair witch project"
];

// Execute on page load.
document.getElementById("movie-title").onload = function() {updateMovieToGuess();};


//Generating random horror movie title

var updateMovieToGuess = function() {
  movieToGuess = movieTitles[Math.floor(Math.random() * movieTitles.length)];

};
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Hangman</title>
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <script scr="javascript/games.js"></script>
<link href="https://fonts.googleapis.com/css?family=IM+Fell+Double+Pica+SC" rel="stylesheet">
</head>

<body>
    <p id='any-key'>Press any key to get started</p>
    <p id='movie-title'>Movie Title:</p>
    <p id='letters'>Letters Guessed:</p>
    <p id='lives-left'>Lives Remaining:</p>
    <p id='wins'>Movies You've Survived: </p>
    <p id='lost'>Movies You Died In:</p>
    
    <footer>
    </footer>
</body>

</html>

最佳答案

嗯,我通常对随机的事情做的是这样的。

var rand = Math.random(0, 100)

if (rand < 'put percentage here') {
print('then do which one you want')
}

这也许可行,并且对每个人都这样做。

关于Javascript onload 不适用于我的随机单词生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56102465/

相关文章:

javascript - 子控件命名标准改变了吗?

javascript - jsRender 模板中的算术运算符

javascript - 为什么我们需要 javascript : while making inline javascript calls

c# - jQuery 不加载响应

javascript - 如何在 AlaSQL 中创建时间字段

javascript - 提交和值更改事件上的 Angular 4 ControlValueAccessor 值

javascript - jquery 从几个多重选择中切换

javascript - 使用 babel 编译多个文件

javascript - JQuery 之后调用 PHP 文件

javascript - 无法使 JavaScript 事件处理程序获取文本框文本属性