简体   繁体   English

如何将Javascript中的函数与条件交织?

[英]How can I interlace functions in Javascript with conditions?

Here's my problem: 这是我的问题:
I'm programming a little game where you need to connect numbers, which are in a table, on your screen with mouse clicks. 我正在编写一个小游戏,您需要用鼠标单击在屏幕上连接表中的数字。 After you pressed the right number, they will be connected by a line, which is a png file and has 0 width and height. 按正确的数字后,它们将通过一条线连接,这是png文件,宽度和高度为0。 That changes after you clicked the cell with the right number. 单击具有正确数字的单元格后,情况将发生变化。 Now, it works, but you could just press the first cell over and over again to get all the lines. 现在,它可以工作,但是您可以反复按第一个单元格以获取所有行。 I'm trying for hours now to find a solution, but nothing works for me. 我现在尝试了几个小时才能找到解决方案,但对我来说没有任何用。
Thank you 谢谢
Here's the code: 这是代码:

cellNumber = 49;
$("#cell_"+cellNumber).click(function()
{
    $("#line_"+lineCounter).attr("width", "600px");
    $("#line_"+lineCounter).attr("height", "440px");
    cellNumber = 65;
    lineCounter++;
    $("#cell_"+cellNumber).click(function()
    {
        $("#line_"+lineCounter).attr("width", "600px");
        $("#line_"+lineCounter).attr("height", "440px");
        cellNumber = 110;
        lineCounter++;
        $("#cell_"+cellNumber).click(function()
        {
            $("#line_"+lineCounter).attr("width", "600px");
            $("#line_"+lineCounter).attr("height", "440px");
            cellNumber = 112;
            lineCounter++;
            $("#cell_"+cellNumber).click(function()
            {
                $("#line_"+lineCounter).attr("width", "600px");
                $("#line_"+lineCounter).attr("height", "440px");
            });
        });
    });
});

You can't nest click events, it's counter-intuitive. 您不能嵌套单击事件,这是违反直觉的。 Click events are bound to objects upon the document ready event and thus must be within the document.ready() function in order to work. 单击事件在文档就绪事件上绑定到对象,因此必须在document.ready()函数中才能起作用。

$(document).ready(function() {
    cellNumber = 49;
    $("#cell_"+cellNumber).click(function() {
        $("#line_"+lineCounter).attr("width", "600px");
        $("#line_"+lineCounter).attr("height", "440px");
        cellNumber = 65;
        lineCounter++;
    });
    $("#cell_"+cellNumber).click(function() {
        $("#line_"+lineCounter).attr("width", "600px");
        $("#line_"+lineCounter).attr("height", "440px");
        cellNumber = 110;
        lineCounter++;
    });
    $("#cell_"+cellNumber).click(function() {
        $("#line_"+lineCounter).attr("width", "600px");
        $("#line_"+lineCounter).attr("height", "440px");
        cellNumber = 112;
        lineCounter++;
    });
    $("#cell_"+cellNumber).click(function() {
        $("#line_"+lineCounter).attr("width", "600px");
        $("#line_"+lineCounter).attr("height", "440px");
    });
});

How about removing click from correctly selected cells; 如何从正确选择的单元格中删除点击;

cellNumber = 49;
    $("#cell_"+cellNumber).on('click', function()
    {
        $("#line_"+lineCounter).attr("width", "600px");
        $("#line_"+lineCounter).attr("height", "440px");
        $("#cell_"+cellNumber).off('click')
        cellNumber = 65;
        lineCounter++;
        $("#cell_"+cellNumber).on('click', function()
        {
            $("#line_"+lineCounter).attr("width", "600px");
            $("#line_"+lineCounter).attr("height", "440px");
           $("#cell_"+cellNumber).off('click')
            cellNumber = 110;
            lineCounter++;
            $("#cell_"+cellNumber).on('click', function()
            {
                $("#line_"+lineCounter).attr("width", "600px");
                $("#line_"+lineCounter).attr("height", "440px");
                $("#cell_"+cellNumber).off('click')
                cellNumber = 112;
                lineCounter++;
                $("#cell_"+cellNumber).click(function()
                {
                    $("#line_"+lineCounter).attr("width", "600px");
                    $("#line_"+lineCounter).attr("height", "440px");
                });
            });
        });
    });

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

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