简体   繁体   English

重新加载后,按钮仅在第二次单击时起作用

[英]Button Only Works on Second Click After Reload

I'm creating a board with tiles that can be clicked to change colors. 我正在创建一个带有可单击以更改颜色的图块的板。 I have a "Clear Board" button that effectively clears the board so all tiles are white again, but for some reason the button only works on the second click after each page reload. 我有一个“清除木板”按钮,可有效清除木板,使所有图块再次变为白色,但由于某种原因,该按钮仅在每次重新加载页面后第二次单击时起作用。 I've tried wrapping my JavaScript in a document ready function but that didn't help. 我尝试将JavaScript包装在文档就绪函数中,但这没有帮助。 How can I get it to work on the first click after a reload? 重新加载后如何使它在第一次点击时起作用?

HTML: HTML:

<h1 class="title">Wacky Painter</h1>
    <div class="easel">
    </div>
    <form class="clear_board">
        <button type="button" id="clear_button" class="btn" onclick="clearBoard()">Clear Board</button>
    </form>

CSS: CSS:

body {
  font-family: sans-serif;
  color: #ff757a;
}

.easel {
  width: 300px;
  outline: #c8c8c8 5px solid;
  margin: 0 auto;
}

.square {
  width: 20px;
  height: 20px;
  outline: thin solid #c8c8c8;
  display: inline-block;
  margin-top:-2px;
}

form {
  margin: 10px auto;
}

.title {
  margin: 0 auto;
  text-align: center;
  padding-bottom: 20px;
}

JavaScript (with jQuery): JavaScript(使用jQuery):

window.setUpEasel = function() {
    var squareString = "";
    for(var i=0; i < 195; i++) {
        squareString+='<div class="square"></div>';
    }
    $('.easel').append(squareString)
}

window.giveColor = function() {
    $('.easel').on('click', '.square', function() {
        var letters = '0123456789ABCDEF'.split('');
        var randomColor = '#';
        for (var i = 0; i < 6; i++ ) {
            randomColor += letters[Math.round(Math.random() * 15)];
        }
        $($(this)).css('background-color', randomColor);
    })
}
window.clearBoard = function() {
    $('#clear_button').on('click', function () {
        $('.square').css('background-color', 'white');
    })
}

    $(function () {
        setUpEasel();
        giveColor();
    });

Here's a working jsfiddle: http://jsfiddle.net/MichelleGlauser/yz6mdx1f/1/ 这是一个工作的jsfiddle: http : //jsfiddle.net/MichelleGlauser/yz6mdx1f/1/

You don't set up your jQuery click event handler on the clear button until you click on the button (using the event handler assigned in the button attributes). 除非单击按钮,否则不要在清除按钮上设置jQuery click事件处理程序(使用在按钮属性中分配的事件处理程序)。 Try this instead: 尝试以下方法:

// code above stays the same

// Adjust so that it is merely responsible for clearing the background
window.clearBoard = function() {
    $('.square').css('background-color', 'white');
};

$(function() {
    setUpEasel();
    giveColor();

    // Assign the click handler on DOM-ready
    $('#clear_button').on('click', function () {
        clearBoard();
    });
});

And finally, remove the inline onclick attribute from your button markup. 最后,从按钮标记中删除内联onclick属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM