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>
答案 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
,而不是onload
:p
。
onload
用于诸如script
,link
,img
等元素-那些正在加载数据的元素。 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();};