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/