简体   繁体   English

在mouseup()事件中使用mousedown()中的变量

[英]Use a var from mousedown() in the mouseup() event

So to those who looked at my previous question , im building a chess board in complete JS and jQuery (or mostly at least). 因此,对于那些看过我上一个问题的人 ,我可以使用完整的JS和jQuery(或至少是大多数)来构建棋盘。

So for my pieces to effectivly be restricted in the amount of squares they are allowed to move i need to know their position. 因此,要让我的作品有效地受到正方形移动量的限制,我需要知道它们的位置。 (starting and ending position) (开始和结束位置)

I wrote the code below to log the starting row (integer) and starting column (integer) and do that on both mousedown() and mouseup() 我写了下面的代码来记录起始行(整数)和起始列(整数),并在mousedown()mouseup()上都这样做

var piece;
        $('div').mousedown(function(e) {
            e.preventDefault();
            var selectedRow = this.getAttribute("data-row");
            var selectedColumn = this.getAttribute("data-column");
            console.log(selectedRow, selectedColumn);
            piece = $(this).find('.pawn');
        })
        .mouseup(function() {
            var selectedRow = this.getAttribute("data-row");
            var selectedColumn = this.getAttribute("data-column");
            console.log(selectedRow, selectedColumn);
            if (selectedRow === selectedRow++ || selectedColumn === selectedColumn++){
                console.log('TRUE :D'); //Wont be true because both selectedRow's will be the same value
            }
            $(this).append(piece);
        });

For as far as i can see i cant compare both values since both logs are in different events. 据我所知,由于两个日志处于不同事件中,因此我无法比较两个值。 (please keep in mind that im new to both languages and im still learning). (请记住,这对两种语言都是新手,而且还在学习中)。

My question would be if its possible to collect both values (starting and ending) and then being able to compare them to each other. 我的问题是是否有可能收集两个值(开始和结束),然后能够将它们相互比较。

The easiest approach is to make a selectedRow_down and selectedColumn_down selectedRow_up and selectedColumn_up in global scope. 最简单的方法是在全局范围内创建selectedRow_down和selectedColumn_down selectedRow_up和selectedColumn_up。

var selectedRow_down;
var selectedColumn_down;
var selectedRow_up;
var selectedColumn_up;

$('div').mousedown(function(e) {
    e.preventDefault();
    var selectedRow_down = this.getAttribute("data-row");
    var selectedColumn_down = this.getAttribute("data-column");
    piece = $(this).find('.pawn');
})
.mouseup(function() {
    var selectedRow_up = this.getAttribute("data-row");
    var selectedColumn_up = this.getAttribute("data-column");
    console.log(selectedRow_up, selectedColumn_up);
    $(this).append(piece);
});

then refer to its value as you do your mouse event 然后在执行鼠标事件时引用其值

also you could make global 2 dimensional array first so could keep track of your chess pieces, see this thread how to create 2d arrays How can I create a two dimensional array in JavaScript? 也可以先制作全局二维数组,以便跟踪棋子,请参见此线程如何创建2d数组。 如何在JavaScript中创建二维数组?

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

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