我正在处理一个编码挑战,要求我使用jQuery创建一个棋盘,以及一个输入表格,询问游戏板的大小(即6x6板、7x7板等)。每当输入是奇数(即7x7板、9x9板、11x11板)时,我都很难分配奇数/偶数类。如果在将输入#增加到任意奇数时运行JS fiddle,则奇数/偶数赋值将"跳过"。
$(document).ready(function() {
//create input form for number/size of board
$('h1').append('<div class=div1>Size of Board: <input type="text" id = "size1" name="size2" min = "2" max = "100" step = "2" value = "6"><input type="submit" id="submit1" value="Create"></div>')
//create button to print Game Pieces
$('h1').append('<div><input type="submit" id="submitForm" value="Lets Play!"></div>');
var z = '';
//on clicking the button, create array of empty boxes/<td>
$('#submit1').click(function() {
var array = [];
//remove previous appended array
$('tbody').empty();
//grab current value or size of gameboard
z = $('#size1').val();
//with a for loop, create "empty" table (z by z) of boxes
for (var i=0; i<z; i++) {
//addClass so we can grab later for color assignment
var trEven = $('<tr>').addClass('trEven');
var trOdd = $('<tr>').addClass('trOdd');
//Differentiate between row to row: assign class trEven and trOdd to every other row
if (i%2 == 0) {
array.push(trEven);
}
else {
array.push(trOdd);
}
//for each row add z number of td's
for (var j=0; j<z; j++) {
array[i].append('<td></td>');
}
}
//append updated array to <tbody>
$('tbody').append(array);
//select all evens/odds of trOdd/trEven to assign colors using special selectors
$('.trOdd td:odd').css('background-color', 'black');
$('.trOdd td:even').css('background-color', 'white');
$('.trEven td:odd').css('background-color', 'white');
$('.trEven td:even').css('background-color', 'black');
});//onclick function
//Play Button: add game pieces
$('#submitForm').click(function(){
if (z <= 6) {
//first two rows
$('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
$('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
//last two rows
$('#gameBoard tr:last td:even').append('<div class="gamePiece">')
$('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
}
else if (z > 6) {
//first three rows
$('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
$('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
$('#gameBoard tr:eq(2) td:odd').append('<div class="gamePiece">')
//last three rows
$('#gameBoard tr:last td:even').append('<div class="gamePiece">')
$('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
$('#gameBoard tr:nth-last-child(3) td:even').append('<div class="gamePiece">')
}
})//onclick function
})//document
table {
border: solid 1px black;
border-spacing: 0px;
}
td {
width: 50px;
height: 50px;
}
.div1 {
font-size: medium;
}
.gamePiece {
border-radius: 100%;
height: 100%;
width: 100%;
background-color: red;
}
<!DOCTYPE html>
<body>
<h1>Game Board</h1>
<table id="gameBoard">
<tbody></tbody>
</table>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</body>
如果您调出检查器工具,单击"跳过"行(例如第三行)并将其与第一行进行比较,则两行都被识别为"偶数"行,但第一个td元素不同。第1行的第一个td/"列"被拉为td:even。然而,第三行的第一个td/"列"被拉为td:odd。据我所知,似乎在每对行(1奇数行和1偶数行)之后,索引都会跳过,第一列应该是"0",而它是"1"。只有当电路板尺寸为奇数时才会出现这种情况。
有人知道为什么跳过索引吗?
使用布尔标志来确定分配类的位置怎么样?
首先,在"创建"按钮点击处理程序中声明一个布尔变量:
var toggleFlag = true;
然后,在您的for
循环中,您"计算"每行中td
的数量,您可以立即分配一个类:
//for each row add z number of td's
for (var j = 0; j < z; j++) {
if(toggleFlag){
array[i].append('<td class="odd"></td>');
}else{
array[i].append('<td class="even"></td>');
}
// Toggle the flag for next iteration
toggleFlag=!toggleFlag;
}
这个循环一结束,就检查每行上是否应该有奇数或偶数的td
。。。因为如果它很奇怪,你必须再次切换:
// If rows contain an even amout of td, toggle again before looping to next row
if(z % 2 == 0){
toggleFlag=!toggleFlag;
}
最后,当您点击"Let's play"按钮添加片段时,您可以使用奇数类而不是:odd
或:even
选择器来选择td
。
$('#gameBoard tr:nth-last-child(3) td.odd').append('<div class="gamePiece">')
查看此工作CodePen
;)
编辑以解释初始错误
代码中的错误位于以下行:
$('.trOdd td:odd').css('background-color', 'black');
$('.trOdd td:even').css('background-color', 'white');
$('.trEven td:odd').css('background-color', 'white');
$('.trEven td:even').css('background-color', 'black');
这种逻辑适用于偶数平方
但如果是奇数,你会致命地得到一个抵消
这就是这种"二乘二"行分组效果的原因
所以你必须有这个代码块的"交替逻辑"。。。在每一行
这并不简单
这就是为什么我在代码的早期就解决了这个问题,在循环中逐个分配循环td
的类。
为了说明这一点,我制作了另一个CodePen
它清楚地表明,该选择器$('.trOdd td:odd')
选择了在奇tr
中具有奇类的ALLtd
但不仅仅是在一排我相信你在想。。。它不像人类那样一排排地排列。它抓取所有的表来收集匹配的元素。
看到了吗?对于jQuery,只有两行(trOdd
和trEven
)
;)
这一切都与CSS有关。尝试突出JS将类分配给奇数和偶数元素,但使用CSS来选择正确的元素。
$(document).ready(function() {
//create input form for number/size of board
$('h1').append('<div class=div1>Size of Board: <input type="text" id = "size1" name="size2" min = "2" max = "100" step = "2" value = "6"><input type="submit" id="submit1" value="Create"></div>')
//create button to print Game Pieces
$('h1').append('<div><input type="submit" id="submitForm" value="Lets Play!"></div>');
var z = '';
//on clicking the button, create array of empty boxes/<td>
$('#submit1').click(function() {
var array = [];
//remove previous appended array
$('tbody').empty();
//grab current value or size of gameboard
z = $('#size1').val();
//with a for loop, create "empty" table (z by z) of boxes
for (var i = 0; i < z; i++) {
//addClass so we can grab later for color assignment
var trEven = $('<tr>').addClass('trEven');
var trOdd = $('<tr>').addClass('trOdd');
//Differentiate between row to row: assign class trEven and trOdd to every other row
if (i % 2 == 0) {
array.push(trEven);
} else {
array.push(trOdd);
}
//for each row add z number of td's
for (var j = 0; j < z; j++) {
array[i].append('<td></td>');
}
}
//append updated array to <tbody>
$('tbody').append(array);
//select all evens/odds of trOdd/trEven to assign colors using special selectors
$('.trOdd td:odd').addClass('even');
$('.trOdd td:even').addClass('odd');
$('.trEven td:odd').addClass('odd');
$('.trEven td:even').addClass('even');
}); //onclick function
//Play Button: add game pieces
$('#submitForm').click(function() {
if (z <= 6) {
//first two rows
$('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
$('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
//last two rows
$('#gameBoard tr:last td:even').append('<div class="gamePiece">')
$('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
} else if (z > 6) {
//first three rows
$('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
$('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
$('#gameBoard tr:eq(2) td:odd').append('<div class="gamePiece">')
//last three rows
$('#gameBoard tr:last td:even').append('<div class="gamePiece">')
$('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
$('#gameBoard tr:nth-last-child(3) td:even').append('<div class="gamePiece">')
}
}) //onclick function
}) //document
table {
border: 1px solid #000;
}
td {
background-color: #fff;
height: 50px;
width: 50px;
}
td:nth-child(2n) {
background-color: #000;
}
tr:nth-child(2n) td {
background-color: #000;
}
tr:nth-child(2n) td:nth-child(2n) {
background-color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Game Board</h1>
<table id="gameBoard">
<tbody></tbody>
</table>