即使从元素中删除 onclick 后,Onclick 仍会继续调用函数

问题描述 投票:0回答:0

我正在创建一个简单的剪刀石头布游戏。 当用户选择一个手势(图像)时,它有 onclick 调用函数来生成一个随机手势(bot)。

问题: 我正在使用 seguent js 代码从所有 (3) 个手势中删除所有 onclicks 5 秒,但它没有按预期工作。

    for(let i = 0; i < gestures.length; i++){
         gestures[i].onclick = null;
    }
    setTimeout(function() {
    for(let i = 0; i < gestures.length; i++){
        gestures[i].onclick = function(){
            Play(this);
        };
    }
}, 5000);

-这段代码在 Play() 函数中-

相对html部分:

                <div class="type">
                    <h2>Rock</h2>
                    <img class="gesture" src="rock.png" id="rock" alt="Rock" width="250px" height="250px" onclick="Play(this)">
                </div>
                <div class="type">
                    <h2>Paper</h2>
                    <div class="wrapper" >
                        <img class="gesture" src="paper.png" id="paper" alt="Paper" width="200px" height="200px" onclick="Play(this)">
                    </div>
                </div>
                <div class="type">
                    <h2>Scissors</h2>
                    <img class="gesture" src="scissors.png" id="scissors" alt="Scissors" width="250px" height="250px" onclick="Play(this)">
                </div>
javascript html settimeout
© www.soinside.com 2019 - 2024. All rights reserved.