我正在尝试创建一个启动按钮,但每当我测试程序时,它会显示但不会执行任何操作。
这就是我所拥有的:
var startGame = function() { //when the start button is clicked, the game starts
$('#startButton').click(function() {
$('#main').hide();
playing = true;
});
}
$(document).ready(startGame()); //when the html loads, start the function
while (playing) {
//this is the main loop for the program
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body id="textHere">
<div id="main">
<h1>I am a computer.</h1>
<a class="button" id="startButton">START</a>
<!--this is the button-->
</div>
<script src="" https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js ""></script>
</script>
<script src='I%20am%20a%20computer.js'></script> <!--where it gets the script-->
</body>
答案 0 :(得分:2)
您正在调用startGame
函数,而不是将其传递给document.ready
。它应该是:
$(document).ready(startGame);
当您将括号放在函数名后面时,它会立即调用它。要将该函数用作值,请忽略括号。