일부 정말 빠른 해결책. 전체 HTML을 붙여 넣었으므로 HTML 파일로 저장하고 브라우저에서 열 수 있습니다. 셀을 클릭하여 좌표 (0 기준)를 볼 수 있습니다. 당신이 rows
및 cols
변수를 변경 한 경우에는 더 큰 그리드를 그릴 수 있다는
는
<html>
<head>
<title>GRID</title>
<style type="text/css">
table tr td { width: 50px; height: 50px; background-color: silver; border: 1px solid black; }
table tr td.over { background-color: yellow; }
table tr td.active { background-color: red; }
.controls { padding: 20px; }
</style>
</head>
<body>
<div class="controls">
<a href="javascript:void(0)" data-move="[-1, 0]">left</a>
<a href="javascript:void(0)" data-move="[0, -1]">top</a>
<a href="javascript:void(0)" data-move="[1, 0]">right</a>
<a href="javascript:void(0)" data-move="[0, 1]">bottom</a>
<a href="javascript:void(0)" data-move="[-1, -1]">topleft</a>
<a href="javascript:void(0)" data-move="[1, -1]">topright</a>
<a href="javascript:void(0)" data-move="[1, 1]">bottomright</a>
<a href="javascript:void(0)" data-move="[-1, 1]">bottomleft</a>
</div>
<table></table>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var rows = 6,
cols = 7;
for(var i = 0; i < rows; i++) {
$('table').append('<tr></tr>');
for(var j = 0; j < cols; j++) {
$('table').find('tr').eq(i).append('<td></td>');
$('table').find('tr').eq(i).find('td').eq(j).attr('data-row', i).attr('data-col', j);
}
}
$('table tr td').mouseover(function() {
$(this).addClass('over');
}).mouseout(function() {
$(this).removeClass('over');
}).click(function() {
$(this).addClass('active');
});
$(".controls a").click(function() {
var $active = $("table tr td.active");
if($active.length > 0) {
var move = $.parseJSON($(this).attr('data-move'));
if(move.length >= 2) {
$active.each(function() {
var row = parseInt($(this).attr('data-row')) + move[1],
col = parseInt($(this).attr('data-col')) + move[0];
if(col >= cols) col = cols - 1;
if(col < 0) col = 0;
if(row >= rows) row = rows - 1;
if(row < 0) row = 0;
$(this).removeClass('active');
$('table tr').eq(row).find('td').eq(col).addClass('active');
});
}
}
});
});
</script>
</body>
</html>
알 수 있습니다.
버튼으로 controls
div를 추가 했으므로 길 찾기를 할 수 있습니다. 먼저 요소를 활성으로 표시해야하며 클릭하면 이동할 수 있습니다.
여러 필드를 표시하고 한꺼번에 이동할 수있는 버그 (또는 내 의견에는 기능)가 하나 있습니다.
시작하기에 좋은 기초입니다!
jQuery를 사용할 수 있습니까? 훨씬 쉬울 것입니다 ... – Karol
idnt는 그것을 사용하는 방법이 아니라 내 코드에 jquery를 가져 오는 방법을 알고 있습니다. –