javascript - 需要在按钮上单击两次或更多次才能触发功能

标签 javascript jquery html css

我知道已经有人问过这类问题,但我找到的答案对我的情况毫无帮助。 所以我有一个函数GameOverScreen(),它会导致点击一个按钮来重置所有内容。 当我点击我的按钮时,我的大部分代码都完成了,但没有一个特定的行:$('#gameOver').css('display', 'none'); 如果我想要这个完成,我必须在我的按钮上点击几次。

请注意,我的 HTML 中没有点击事件,它是一个简单的 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">的东西?

关于javascript - 需要在按钮上单击两次或更多次才能触发功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43226868/

相关文章:

javascript - 测试字符串是否包含字符串数组中的单词

javascript - 抓取一些表格数据时得到奇怪的输出

php - 错误消息 "Unterminated string literal"

Javascript 数组访问在 IE 7 中不起作用

html - 如何使用 Visual Studio Code 在浏览器中查看 HTML 文件

css - IE 不支持 DIV 100% 高度

javascript - .click of class 也得到一个输入

javascript - 为什么我提交时无法获取 POST/GET 文本框

javascript - Struts2 jsp页面在新窗口中打开而不是内容div

javascript - 用 jQuery 实现 "this is taking too long"消息