2016-08-04 40 views
4

我正在編寫一個挑戰,要求我使用jQuery創建一個棋盤,以及一個輸入表單,詢問遊戲板的大小(即6x6板,7x7板,等等。)。每當輸入是奇數時(即7x7板,9x9板,11x11板),我都很難進行奇數/偶數類分配。如果在將輸入#增加到任何奇數時運行JS小提琴,奇數/偶數分配將「跳過」。製作一個動態的棋盤響應用戶輸入jQuery

$(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) { 
 
\t //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」。這僅在電路板尺寸奇怪時纔會發生。

有誰知道爲什麼索引被跳過?

回答

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; 
} 

最後,當你點擊了「讓我們玩」按鈕來添加件,則可以使用奇數類,而不是:odd:even選擇的選擇td

$('#gameBoard tr:nth-last-child(3) td.odd').append('<div class="gamePiece">') 

See this working 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的循環中的類逐個地在代碼中儘早解決問題。

爲了說明這一點,我製作了another CodePen
它清楚地表明,這個選擇器$('.trOdd td:odd')選擇全部td奇數類在奇數tr ... 但不僅在一行!就像我確定你在想......它並不像人類一樣一行一行地排列。它抓住所有的表來收集匹配的元素。

請參閱? jQuery的,只有兩行(trOddtrEven)...
;)

+0

謝謝你的解決方案,因爲我我從這種方法中學到了很多東西,我知道使用一個標誌來指定下一行將如何開始;黑色或白色但是我之前對這個問題的疑問(似乎是索引問題?)沒有回答,據我所知,我的代碼應該適用於一個奇數大小的板子 – jaysonder

+0

我從你的代碼中學到了很多東西,對我來說這種方法是「開箱即用」的,乾杯! – jaysonder

+0

我尊重那個;)你想明白你的錯誤......我試着在我最後的編輯中儘可能清楚地回答。 –

1

這都是關於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>

+0

嘿,你將有你的鼠標輸入表單和UP和DOWN按鈕應該出現那麼這將創建懸停板。 – jaysonder

+0

謝謝,我看了你的代碼片段,發現你的CSS只是指向div.odd和div.even,所以這將使CSS難以確定哪些行是div的。你可以改變你的CSS爲' .trOdd .odd','trOdd .even'和'.trEven。奇怪','.trEven .even' – noob

+0

我改變了我的CSS,但仍遇到同樣的問題。如果拉起檢查器工具並單擊「跳過」行(例如第三行)並將其與第一行進行比較,則兩行都具有偶數類,但第一個TD元素不同。第一行的第一個​​被賦予「偶數」,而第三行被賦予「奇數」。 – jaysonder