【发布时间】:2015-10-31 10:56:45
【问题描述】:
我需要编写一个脚本来通过单击(变为红色)选择黑色 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 的白盒,此时值与红色正确交换它是选择的,但是如果您尝试重新交换两个白色值,那是可行的,但没有红色。
非常感谢
【问题讨论】:
-
如果我单击 12 并单击白色大小写,它确实会更改值,但之后即使我单击 39 并单击白色 div 也会将 12 放入白色 div 而不是 39
-
感谢您的回答,也许您在白盒上移动 12 后单击 12,然后在黑盒上单击 39。我需要解决这个问题,我需要在选择第一个 div 时捕获值(并变为红色),然后再单击一次。
标签: javascript jquery swap