一个翻翻的小游戏。HTML式。

javascript 复制代码
<!DOCTYPE html>
<html>
<head>
    <style>
    	#grid{
    		width:316px;
    		height: 316px;
    		 background-color: #00BFFF;    		
    	}
        .cell {
            width: 60px;
            height: 60px;
            border: 1px solid black;
            float: left;           
        }
        .red {
            background-color: red;
        }
        .black {
            background-color: black;
        }
    </style>
</head>
<body>
    <div id="grid"></div>
    <script>
        var grid = [];
        var size = 5;
        var gridElement = document.getElementById('grid');

        for (var i = 0; i < size; i++) {
            var row = [];
            for (var j = 0; j < size; j++) {
                var cell = document.createElement('div');
                cell.className = 'cell red';
                cell.onclick = (function(i, j) {
                    return function() {
                        toggleColor(i, j);
                    }
                })(i, j);
                gridElement.appendChild(cell);
                row.push(cell);
            }
            grid.push(row);
        }

        grid[2][2].className = 'cell black';

        function toggleColor(i, j) {
            var directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
            for (var k = 0; k < directions.length; k++) {
                var ni = i + directions[k][0];
                var nj = j + directions[k][1];
                if (ni >= 0 && ni < size && nj >= 0 && nj < size) {
                    var cell = grid[ni][nj];
                    cell.className = cell.className === 'cell red' ? 'cell black' : 'cell red';
                }
            }
        }
    </script>
</body>
</html>
相关推荐
独泪了无痕3 小时前
使用Fetch API 探索前后端数据交互
前端·http·交互设计
css趣多多3 小时前
别名路径的知识点
前端
靓仔建5 小时前
Vue3导入组件出错does not provide an export named ‘user_setting‘ (at index.vue:180:10)
开发语言·前端·typescript
EnoYao5 小时前
我写了一个团队体检报告 Skill,把摸鱼的同事扒出来了😅
前端·javascript
梁正雄5 小时前
Python前端-2-css练习
前端·css·python
清汤饺子5 小时前
用 Cursor 半年了,效率还是没提升?是因为你没用对这 7 个功能
前端·后端·cursor
Never_Satisfied5 小时前
在JavaScript / Node.js中,package.json文件中的依赖项自动选择最新版安装
javascript·node.js·json
蓝莓味的口香糖5 小时前
【vue3】组件的批量全局注册
前端·javascript·vue.js
wefly20176 小时前
开发者效率神器!jsontop.cn一站式工具集,覆盖开发全流程高频需求
前端·后端·python·django·flask·前端开发工具·后端开发工具
独泪了无痕6 小时前
自动导入 AutoImport:告别手动引入依赖,优化Vue3开发体验
前端·vue.js·typescript