我有一個字謎遊戲,我試圖在輸入字母后自動將光標移動到下一個字母框。我可以做到這一點,但不知道下來。我希望以優先級#1的形式出現,如果下一個框不是信箱,它將搜索它是否可以關閉。如果它不能穿越或下降,那麼什麼都不會發生。jQuery獲取當前下方表格中的下一個元素
以下是我對整個去:
$('.crossword_puzzle input[type="text"]').keyup(function() {
$(this).parent().parent().next().find('input[type="text"]').focus().select();
});
我怎麼能走下來,如果對面沒有找到?
DOM是一個在每個TD中都有輸入框的html表。
感謝您的幫助。
編輯 - 繼承人是表的一部分,忘了我在每個td也有一組div。還有一個div在每個單詞的開頭都有一個數字。
<table class="crossword_puzzle">
<tr>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="block_num">1</div>
<input type="text" class="word_3_1 cp_move_across" maxlength="1" onClick="this.select();" id="x3y1" name="x3y1" value=""/>
<div class="block_num">1</div>
</div>
</td>
<td>
<div class="all_blocks"><input type="text" class="word_3_1 cp_move_across" maxlength="1" onClick="this.select();" id="x4y1" name="x4y1" value=""/></div>
</td>
<td>
<div class="all_blocks">
<input type="text" class="word_3_1 cp_move_across" maxlength="1" onClick="this.select();" id="x5y1" name="x5y1" value=""/>
<div class="block_num">2</div>
</div>
</td>
<td>
<div class="all_blocks"><input type="text" class="word_3_1 cp_move_across" maxlength="1" onClick="this.select();" id="x6y1" name="x6y1" value=""/></div>
</td>
<td>
<div class="all_blocks"><input type="text" class="word_3_1 cp_move_across" maxlength="1" onClick="this.select();" id="x7y1" name="x7y1" value=""/></div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
</tr>
<tr>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks"><input type="text" class="word_3_1 cp_move_down" maxlength="1" onClick="this.select();" id="x3y2" name="x3y2" value=""/></div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks"><input type="text" class="word_5_1 cp_move_down" maxlength="1" onClick="this.select();" id="x5y2" name="x5y2" value=""/></div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
<div class="black_block"></div>
</div>
</td>
<td>
<div class="all_blocks">
這是怎麼回事,下面@Kevin B答案和添加一個標誌來切換優先順序。有沒有人看到這樣做的問題?編輯:添加的邏輯翻轉基於方向優先如果下一個單元格具有文本或不在當前優先順序
var priority_direction = 'across';
$('.crossword_puzzle input[type="text"]').keyup(function() {
if(priority_direction == 'across'){
var next = $(this).closest('td').next().find('input[type="text"]');
priority_direction = 'across';
if (!next.length || next.val().length == 1) {
var cellIndex = $(this).closest('td').index();
next = $(this).closest('tr').next().children().eq(cellIndex).find('input[type="text"]');
priority_direction = 'down';
}
} else {
var cellIndex = $(this).closest('td').index();
var next = $(this).closest('tr').next().children().eq(cellIndex).find('input[type="text"]');
priority_direction = 'down';
if (!next.length || next.val().length == 1) {
next = $(this).closest('td').next().find('input[type="text"]');
priority_direction = 'across';
}
}
next.focus().select();
});
請發佈標記。 – undefined 2013-03-26 21:46:43
我們是否應該從中猜測HTML結構? – adeneo 2013-03-26 21:47:04
使用':eq()'可以更容易完成。如果你想向右移動,只需增加索引i':eq(i)',如果你想下去,把單元格/行的數目加到那個數字':eq(i + n)'上。 – Cristy 2013-03-26 21:48:03