简体   繁体   English

在Javascript中使用addEventListener()出现问题

[英]Issue using addEventListener() in Javascript

I am making a battleships game, and I am currently working on placing your own boats. 我正在制作一艘战舰游戏,目前正在努力放置自己的船只。 I want to be able to click, and one square on the grid to change color. 我希望能够单击,然后在网格上单击一个正方形即可更改颜色。 I am adding event listeners to each tile through a nested for() loop, and getting each button to reference itself when the function is run. 我正在通过嵌套的for()循环向每个磁贴添加事件侦听器,并在运行函数时让每个按钮引用自身。

The issue is, some of the tiles are changing color when I click on them, but others are not, and there is no pattern I can make out as to which change and which do not, as sometimes, I can click two or three times, and it will change color. 问题是,当我单击它们时,某些磁贴正在改变颜色,而其他磁头却没有改变,并且我无法辨别出哪些改变和哪些没有改变的图案,有时我可以单击两次或三次,它将改变颜色。 The code is here. 代码在这里。 (I also added a color selector). (我还添加了一个颜色选择器)。

    var x_client = 0;
var y_client = 0;
var battlefield_client = "";

for (y_client = 1; y_client < 11; y_client++) {
    battlefield_client += "<tr>";
    for (x_client = 1; x_client < 11; x_client++) {
        battlefield_client += "<td onclick = '' class = 'tile' style='border: 3px solid black;' id=" + "cell_client_" + x_client + "_" + y_client + "><pre>     </pre></td>";
    }
    battlefield_client += "</tr>";
}
$(document).ready(function() {
    $("#tableGrid_client").html(battlefield_client); //loads table

    for (y_client = 1; y_client < 11; y_client++) {
        for (x_client = 1; x_client < 11; x_client++) {
            boatStatusClient = document.getElementById('cell_client_' + x_client + "_" + y_client);
            boatStatusClient.addEventListener("click", function(){boatGrid.placeBoat_client()});
        }
    }
    document.getElementById("redButton").addEventListener("click", function(){boatGrid.colorSelect()});
    document.getElementById("orangeButton").addEventListener("click", function(){boatGrid.colorSelect()});
    document.getElementById("yellowButton").addEventListener("click", function(){boatGrid.colorSelect()});
    document.getElementById("greenButton").addEventListener("click", function(){boatGrid.colorSelect()});
    document.getElementById("blueButton").addEventListener("click", function(){boatGrid.colorSelect()});
    document.getElementById("violetButton").addEventListener("click", function(){boatGrid.colorSelect()});
    document.getElementById("brownButton").addEventListener("click", function(){boatGrid.colorSelect()});
    document.getElementById("blackButton").addEventListener("click", function(){boatGrid.colorSelect()});
}); //This is for the color selector.

    colorSelect : function() {
        colorPick = event.target || event.srcElement;
        colorPick = colorPick.id
        console.log(colorPick);
        if (colorPick == redButton) {
            colorSelect = "red";
        }
        else if (colorPick == orangeButton) {
            colorSelect = "orange";
        }
        else if (colorPick == yellowButton) {
            colorSelect = "yellow";
        }
        else if (colorPick == greenButton) {
            colorSelect = "green";
        }
        else if (colorPick == blueButton) {
            colorSelect = "blue";
        }
        else if (colorPick == violetButton) {
            colorSelect = "purple";
        }
        else if (colorPick == brownButton) {
            colorSelect = "brown";
        }
        else if (colorPick == blackButton) {
            colorSelect = "black";
        }
        console.log(colorSelect);
    },
    placeBoat_client : function() {
        var demoColor = "orange"
        var source_client = event.target.id;
        console.log(source_client);
        source_client.id = document.getElementById(source_client.id);
        document.getElementById(source_client).style.backgroundColor = demoColor;

    },
}

I have applied some fixes: 我已经应用了一些修复程序:

-Loops now use local variables to count iteration, so loop runs and uses its own variable. -循环现在使用局部变量来计数迭代,因此循环运行并使用其自己的变量。 Next loop is not allowed to write over a variable in a different scope. 下一循环不允许覆盖其他范围内的变量。

-You were adding anonymous functions were just function names were expected as parameters. -您正在添加匿名函数,只是将函数名用作参数。 The second parameter inside an 'addEventListener' sets a call just receiving function name, otherwise it is redundant. “ addEventListener”内部的第二个参数设置仅接收函数名称的调用,否则它是多余的。

-Changed a variable name to make it different from selectColor() . -更改了变量名称,使其与selectColor()不同。

-Functions & variables on top of script, so are the first thing to be read. -位于脚本顶部的函数和变量,因此首先要阅读。

-Small things like missing semicolons, extra commas, brackets, etc. -小内容,例如缺少分号,多余的逗号,括号等。

Here you go, hope it helps: 到这里,希望对您有所帮助:

//var x_client = 0;
//var y_client = 0;
var battlefield_client = "";
var setColor='';

    function colorSelect (){
        colorPick = event.target || event.srcElement;
        colorPick = colorPick.id;
        console.log(colorPick);
        if (colorPick == redButton) {
            setColor = "red";
        }
        else if (colorPick == orangeButton) {
            setColor = "orange";
        }
        else if (colorPick == yellowButton) {
            setColor = "yellow";
        }
        else if (colorPick == greenButton) {
            setColor = "green";
        }
        else if (colorPick == blueButton) {
            setColor = "blue";
        }
        else if (colorPick == violetButton) {
            setColor = "purple";
        }
        else if (colorPick == brownButton) {
            setColor = "brown";
        }
        else if (colorPick == blackButton) {
            setColor = "black";
        }
        console.log(setColor);
    }
    //---end of colorSelect

    function placeBoat_client () {
        var demoColor = "orange";
        var source_client = event.target.id;
        console.log(source_client);
        source_client.id = document.getElementById(source_client.id);
        document.getElementById(source_client).style.backgroundColor = demoColor;

    }




for (var y_client = 1; y_client < 11; y_client++) {
    battlefield_client += "<tr>";

    for (var x_client = 1; x_client < 11; x_client++) {
        battlefield_client += "<td onclick = '' class = 'tile' style='border: 3px solid black;' id=" + "cell_client_" + x_client + "_" + y_client + "><pre>     </pre></td>";
    }
    battlefield_client += "</tr>";
}
$(document).ready(function() {
    $("#tableGrid_client").html(battlefield_client); //loads table

    for (var y_client = 1; y_client < 11; y_client++) {

        for (var x_client = 1; x_client < 11; x_client++) {
            boatStatusClient = document.getElementById('cell_client_' + x_client + "_" + y_client);
            boatStatusClient.addEventListener("click", /*boatGrid.*/placeBoat_client, false);
        }
    }

    document.getElementById("redButton").addEventListener("click", /*boatGrid.*/colorSelect, false);
    document.getElementById("orangeButton").addEventListener("click", /*boatGrid.*/colorSelect, false);
    document.getElementById("yellowButton").addEventListener("click", /*boatGrid.*/colorSelect, false);
    document.getElementById("greenButton").addEventListener("click", /*boatGrid.*/colorSelect, false);
    document.getElementById("blueButton").addEventListener("click", /*boatGrid.*/colorSelect, false);
    document.getElementById("violetButton").addEventListener("click", /*boatGrid.*/colorSelect, false);
    document.getElementById("brownButton").addEventListener("click", /*boatGrid.*/colorSelect, false);
    document.getElementById("blackButton").addEventListener("click", /*boatGrid.*/colorSelect, false);

}); //end of ready

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

相关问题 使用addEventListener将JavaScript添加到表单时出现问题 - Issue adding javascript to a form using addEventListener JavaScript AddEventListener问题 - Javascript AddEventListener issue 在Javascript中将getAttribute与addEventListener一起使用 - Using getAttribute with addEventListener in Javascript 将此关键字与javascript原型addEventListener一起使用 - using this keyword with javascript prototype addEventListener addEventListener 使用 Javascript 创建待办事项列表的问题 - addEventListener issue creating To Do list with Javascript 如何解决问题“addEventListener”“click”在 javascript 中不起作用, - how to resolve issue "addEventListener" "click" not working in javascript, laravel foreach 循环中的Javascript addEventListener 问题 - Javascript addEventListener issue in laravel foreach loop 在 JavaScript class 方法中,即使使用箭头函数也无法在 addEventListener() 中使用“this”。 如何解决这个问题 - In JavaScript class method, Unable to use 'this' inside addEventListener() even using arrow functions. how to solve this issue addEventListener 的问题 - Issue with an addEventListener 使用 document.addEventListener(&#39;DOMContentLoaded&#39;, - Design issue using document.addEventListener('DOMContentLoaded',
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM