我的随机单词生成器无法使用Javascript onload

时间:2019-05-12 18:52:58

标签: 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>

4 个答案:

答案 0 :(得分:1)

嗯,我通常对随机事物所做的就是这个。

var rand = Math.random(0, 100)

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

这也许行得通,而且每个人都能做到。

答案 1 :(得分:0)

这将获得一个要存储在movieToGuess

中的变量

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

// Execute on page load.
window.onload = function() {updateMovieToGuess();};


//Generating random horror movie title

var updateMovieToGuess = function() {
  movieToGuess = movieTitles[Math.floor(Math.random() * movieTitles.length)];
 console.log(movieToGuess);
};
<!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>

答案 2 :(得分:0)

对于DOMContentLoaded,您应该使用document,而不是onloadp

onload用于诸如scriptlinkimg等元素-那些正在加载数据的元素。 p不会加载任何数据,因此不会触发onload

DOMContentLoaded可以使用时,会触发

DOM事件,因此由您选择! :)

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


//Generating random horror movie title
function updateMovieToGuess() {
  movieToGuess = movieTitles[Math.floor(Math.random() * movieTitles.length)];
  console.log(movieToGuess) 
}

document.addEventListener("DOMContentLoaded", updateMovieToGuess)
<!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>

答案 3 :(得分:0)

您尝试在onload而不是整个页面上触发#movie-title事件,这是错误。

只需:

window.onload = function() {updateMovieToGuess();};