I've searched on here plenty of places and I have not been able to find my situation, so here goes.
I am trying to avoid using the html inline onclick, so in doing so I am using the jquery click() like so
$(document).ready(function(){
$("#start").click(game());
});
When I use this, the function game() runs automatically and only runs and load time.
If I use the inline onclick, it works only when I click, and it works every time I click. Why is this?
Edit.. I swear I tried that.. oh well, It worked!!!
You are calling the game()
function and passing its result to .click()
. Since its result is (presumably) not a function you have not actually set up a click handler at all. You need to pass a reference to the function, so remove the parentheses:
$("#start").click(game);
You are executing game()
immediately by adding the parenthesis. If you want game
to be evaluated on click then you need
$(document).ready(function(){
$("#start").click(game);
});
您需要将其更改为:
$("#start").click(game);
Always more advisable to use .on & an anonymous function since it takes confusions like these out of the picture;
$(document).ready(function() {
$('#start').on('click', function () {
game();
});
}
);
What you're doing would be similar to
$(document).ready(function() {
$('#start').on('click', function () {
game();
}());
}
);
Maybe youe need define game this way:
var game = function(e,...)
$("#start").click(game);
And remember the first parameter is the event object, passed from click function.
Regards.
You could use a jquery function in a html element.
<canvas id="game" onclick="jqueryFunction()"></canvas>
In the jqueryFunction() make a if else statement so if game is running : fire.
else if game not running :load game.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.