javascript - jQuery : swap two value and change style

标签 javascript jquery swap

我需要制作一个脚本来通过单击(变红)选择黑色div,并通过另一次单击将黑色div值放入白色div值中,这是可以的,但是当我尝试交换两个白色大小写的值时,更改一次正确执行,但如果我重新尝试交换白色大小写的两个值,这些值会正确交换,但背景颜色不为红色。

这是我的代码:

var lastClicked = '';
var lastClicked2 = '';

$(".blackcase").click(function(e) {
    var i = 0;
    if ($(this).html().length == 0) {
        return false;
    } else {
        e.preventDefault();
        $('.blackcase').removeClass('red');

        if (lastClicked != this.id) {
            $(this).addClass('red');
            var currentId = $(this).attr('id');
            var currentVal = $(this).html();

            $(".whitecase").click(function(e) {
                $('.blackcase').removeClass('red');
                var currentId2 = $(this).attr('id');

                if (i <= 0 && $("#" + currentId2).html().length == 0) {
                    $("#" + currentId2).html(currentVal);
                    $("#" + currentId).html("");
                    i = 1;
                }
            });
        } else {
            lastClicked = this.id;
        }
    }
});

$(".whitecase").click(function(e) {
    var j = 0;
    if ($(this).html().length == 0) {
        return false;
    } else {
        e.preventDefault();
        $('.whitecase').removeClass('red');

        if (lastClicked2 != this.id) {
            $(this).addClass('red');
            var currentId0 = $(this).attr('id');
            var currentVal0 = $(this).html();

            $(".whitecase").click(function(e) {
                e.preventDefault();
                var currentId02 = $(this).attr('id');
                var currentVal02 = $(this).html();

                if (j <= 0 && currentVal0 != currentVal02) {
                    $('.whitecase').removeClass('red');
                    $("#" + currentId02).html(currentVal0);
                    $("#" + currentId0).html(currentVal02);
                    j = 1;

                    return false;
                }
            });
        } else {
            lastClicked2 = this.id;
        }
    }
});

这是 JSfiddle : https://jsfiddle.net/12gwq95u/12/

尝试将 12 放入第一个白色盒子中,将 39 放入第二个白色盒子中,单击带有 12 的白色盒子(变为红色),然后单击带有 39 的白色盒子,当这些值与红色正确交换时它是选择的,但如果您尝试重新交换两个白写值,这是可行的,但没有红色。

非常感谢

最佳答案

我花了一些时间重写你的代码以使其更加清晰。我不知道您的代码到底应该做什么,但根据您已经提供的信息,我的代码版本如下:

var selectedCase = {color: "", id: ""};

function removeSelectionWithRed() {
   $('div').removeClass('red'); 
}

function selectWithRed(element) {
    removeSelectionWithRed();
    element.addClass('red');
}

function updateSelectedCase(color, id) {
    selectedCase.color = color;
    selectedCase.id = id;
}

function moveValueFromTo(elemFrom, elemTo) {
    elemTo.html(elemFrom.html());
    setValueToElem("", elemFrom);
}

function setValueToElem(value, elem) {
    elem.html(value);
}

function swapValuesFromTo(elemFrom, elemTo) {
    var fromValue = elemFrom.html();
    var toValue = elemTo.html();
    setValueToElem(fromValue, elemTo);
    setValueToElem(toValue, elemFrom);
}

function isSelected(color) {
    return selectedCase.color == color;
}

function clearSelectedCase() {
    selectedCase.color = "";
    selectedCase.id = "";
}

function elemIsEmpty(elem) {
   return elem.html().length == 0;
}

$(".blackcase").click(function (e) {
    if (elemIsEmpty($(this))) {
        return;
    }

    alert("black is selected");
    selectWithRed($(this));
    updateSelectedCase("black", $(this).attr("id"), $(this).html());
});

$(".whitecase").click(function (e) {    
    removeSelectionWithRed();

    if (isSelected("black")) {
        alert("moving black to white");

        moveValueFromTo($("#"+selectedCase.id), $(this));

        clearSelectedCase();
        return;
    }

    if(isSelected("white") && selectedCase.id !== $(this).attr("id")) {
        alert("swap whitecase values");

        swapValuesFromTo($("#"+selectedCase.id), $(this));

        clearSelectedCase();
        return;
    }

    alert("white is selected");
    selectWithRed($(this));
    updateSelectedCase("white", $(this).attr("id"), $(this).html());
});

jsfiddle 链接:https://jsfiddle.net/12gwq95u/21/

如果我的回答有帮助,请点赞。

关于javascript - jQuery : swap two value and change style,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33450763/

相关文章:

c++ - 使用 unique_ptr 交换类

javascript - 使用 PHP 和 JQuery 编辑 HTML 表模板

javascript - Webpack:如何直接导出到包含样式表导入的全局(没有.default)?

javascript - focus() 方法在 jquery 中不起作用

jquery - 使用甜蜜警报防止表单提交时出现默认情况

java - 我做了一个 for 循环来交换字符串中的两个 indiv char 变量

c++ - std::swap of std::priority_queue with lambda comparator 在 C++20 但不是 C++17 下编译: "error: object of type value_compare cannot be assigned"

javascript - 有没有办法通过列类型/属性来格式化 MySQL celldata?

javascript - javascript 表单中 grabFormSelects() 的用途是什么?

javascript - 想使用ajax获取条件数据