[英]Need to click twice or more on a button to trigger a function
我知道已經問過這種類型的問題,但是我發現的答案對我的情況沒有幫助。 因此,我有一個GameOverScreen()
函數,該函數導致單擊按鈕以重置所有內容。 當我點擊按鈕時,我的大部分代碼都完成了,但是沒有一行: $('#gameOver').css('display', 'none');
如果要完成此操作,則必須單擊幾次按鈕。
請注意,我的HTML中沒有click事件,這是一個簡單的p
,內部有一個輸入:
<p>Pour recommencer la partie, cliquez sur le bouton :<input type="reset" id="restart" value="Recommencer"></p>
在此先感謝您的幫助,這是我的jQuery代碼:
function gameOverScreen() {
$('#gameOver').css('display', 'block');
propSound[0].pause();
$('#restart').click(reset);
};
function reset() {
//je remets mon bouton en position "on"
$('#start').css('margin-left', '0px');
//mon avion reprend sa position initiale
planeHeight.css('display', 'block');
planeHeight.css('top', '434px');
planeHeight.css('transform', 'rotateZ(0deg)');
//J'arrête l'animation de mon hélice
clearInterval(intervalIdAnimProp);
//J'arrête l'animation de mon bg
clearInterval(intervalIdanimBg);
//J'arrête le son
propSound[0].pause();
//Le compteur se remet à zéro
speed = '000';
$('#speedometer').val(speed);
//Désaffichage des mes flèches
$('#arrows').css('display', 'none');
$('#display').val("Afficher");
//background
$('#gameOver').css('display', 'none');
bgSrc.css('background', 'url("sources/images/fond.jpg")');
};
編輯:如果需要,這是我的HTML:
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Mini jeux</title>
<script src="jquery-3.1.1.js"></script>
<script src="exercice17.js"></script>
<link rel="stylesheet" href="exercice17.css">
</head>
<body>
<div id="main">
<div id="playground">
<div id="plane">
<img src="sources/images/avion1.png">
<img id="prop" src="sources/images/helice.png">
</div>
<audio id="engine" src="sources/son/moteur.mp3" loop></audio>
<div id="arrows">
<section id="up"><img src="sources/images/boutons.png"></section>
<section id="down"><img src="sources/images/boutons.png"></section>
<section id="right"><img src="sources/images/boutons.png"></section>
<section id="left"><img src="sources/images/boutons.png"></section>
</div>
</div>
<div id="cmd">
<div id="onOff">
<span>Démarrer/arrêter :</span>
<div id ="on"><img id="start" src="sources/images/on-off.png"></div>
</div>
<div id="compteur">
<span>Vitesse :</span>
<input type="text" id="speedometer" value="000" size="1" max='270'>
<p>km/h</p>
</div>
<div id="instructions">
<p>Après avoir démarrré, utilisez les flèches du clavier pour piloter l'avion. Sinon, vous pouvez activer/ désactiver les commandes tactiles : <input type="button" id="display" value="Afficher"></p> </div>
<div id="flyButton">
</div>
</div>
<div id="gameOver">
<h1>GAME OVER</h1>
<p>Pour recommencer la partie, cliquez sur le bouton :<input type="reset" id="restart" value="Recommencer"></p>
</div>
</div>
</body>
我不確定這個答案,但是<input type="reset">
用於將所有表單(如果有)值重置為默認值。 我不認為您要在這里這樣做。 您可以嘗試將輸入按鈕的type
更改為<input type="button">
嗎?
如果隱藏其可見性,它也可能會更好地工作。 (由於您的代碼看起來非常具體,因此不能完全確定)。
就像是:
$('#gameOver').css('visibility', 'hidden');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.