英文:
grid not allowed to be clicked twice
问题
我有一个名为“gridbox”的网格,项目“box1”不应允许第二次点击。这是为了我的井字游戏。
在函数check()中,应该阻止第二次点击该方块。
```html
<section class="gridbox">
<div class="box1" onclick="check()"></div>
</section>
<details>
<summary>英文:</summary>
I have a grid called "gridbox" and the item "box1" should not be allowed to get clicked a 2nd time. Its for my Tik Tak Toe game.
In the function check(), it should prevent the box from getting clicked a 2nd time.
<section class="gridbox">
<div class="box1" onclick="check()"></div>
</section>
</details>
# 答案1
**得分**: 0
欢迎来到Stack Overflow!
在没有你的其他代码的情况下,我不知道你如何准确地存储井字棋棋盘上每个单元格的状态,所以我使用了一个数组来表示它。
我们将创建一个名为`boardState`的变量来表示棋盘上每个单元格的X和O状态:
```js
// board state将包含井字棋棋盘上每个单元格的当前状态
// 可以是'', 'X'或'O',其中''表示空单元格
//
// 这行代码基本上创建了一个包含9个元素的数组(每个单元格一个元素)
// 并将每个元素设置为''。
var boardState = Array(9).fill('')
你的check
函数将看起来像这样:
// 我们的check函数将检查是否可以在该单元格放置标记
function check(target, i) {
if (boardState[i] === '') { // 如果单元格为空
boardState[i] = currentTurn // 更新boardState
target.innerHTML = currentTurn // 填充<div>的值
// 切换到另一个玩家的回合
if (currentTurn === 'X') {
currentTurn = 'O'
} else {
currentTurn = 'X'
}
}
}
这个函数接受两个参数,
target
:被点击的元素i
:被点击的单元格的索引
现在,在调用check
函数时,你可以传递每个单元格的目标和索引到你的onclick
事件中,棋盘上的每个
<section class="gridbox">
<div class="box1" onclick="check(this, 0)"></div>
<div class="box1" onclick="check(this, 1)"></div>
<div class="box1" onclick="check(this, 2)"></div>
</section>
现在,当你点击
这是一个用于3个框的基本工作示例的完整代码:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-html -->
<style>
div.box1 {
width: 100px;
height: 100px;
border: solid 1px black;
/* 这部分是为了居中文本 */
display: flex;
align-items: center;
justify-content: center;
}
</style>
<script>
// board state将包含井字棋棋盘上每个单元格的当前状态
// 可以是'', 'X'或'O',其中''表示空单元格
//
// 这行代码基本上创建了一个包含9个元素的数组(每个单元格一个元素)
// 并将每个元素设置为''。
var boardState = Array(9).fill('')
var currentTurn = 'X'
// 我们的check函数将检查是否可以在该单元格放置标记
function check(target, i) {
if (boardState[i] === '') { // 如果单元格为空
boardState[i] = currentTurn
target.innerHTML = currentTurn
if (currentTurn === 'X') {
currentTurn = 'O'
} else {
currentTurn = 'X'
}
}
}
</script>
<section class="gridbox">
<div class="box1" onclick="check(this, 0)"></div>
<div class="box1" onclick="check(this, 1)"></div>
<div class="box1" onclick="check(this, 2)"></div>
</section>
<!-- 结束代码片段 -->
英文:
Welcome to Stack Overflow!
Without the rest of your code I don't know exactly how you're storing the state of each cell on the Tic Tac Toe board so I used an array to represent it.
We'll create a variable called boardState
to represent the X,O state of each cell on the board:
// board state will contain the current state of each cell
// of the tic tac toe board. '','X', 'O', where '' indicates
// an empty cell
//
// This line is basically creating an array of 9 elements (one for each cell)
// and setting each to ''.
var boardState = Array(9).fill('')
Your check
function will look something like this:
// our check function will check whether or not the cell can have the marker placed
function check(target, i) {
if (boardState[i] === '') { // if the cell is empty
boardState[i] = currentTurn // update boardState
target.innerHTML = currentTurn // fill <div> with value
// switch to other person's turn
if (currentTurn === 'X') {
currentTurn = 'O'
} else {
currentTurn = 'X'
}
}
}
This function takes 2 arguments,
target
: The element that was clickedi
: The index of the cell that was clicked
Now, when calling the check
function on your onclick
event, you can pass the target and index of each, with each div on the board having an increasing index. I've created 3 divs here that show what I'm talking about:
<section class="gridbox">
<div class="box1" onclick="check(this, 0)"></div>
<div class="box1" onclick="check(this, 1)"></div>
<div class="box1" onclick="check(this, 2)"></div>
</section>
Now when you click on the div it'll fill in the value only if it's already empty - otherwise, nothing will happen.
Here's the full code to get a basic working example for 3 boxes:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<style>
div.box1 {
width: 100px;
height: 100px;
border: solid 1px black;
/* This part is to center the text */
display: flex;
align-items: center;
justify-content: center;
}
</style>
<script>
// board state will contain the current state of each cell
// of the tic tac toe board. '','X', 'O', where '' indicates
// an empty cell
//
// This line is basically creating an array of 9 elements (one for each cell)
// and setting each to ''.
var boardState = Array(9).fill('')
var currentTurn = 'X'
// our check function will check whether or not the cell can have the marker placed
function check(target, i) {
if (boardState[i] === '') { // if the cell is empty
boardState[i] = currentTurn
target.innerHTML = currentTurn
if (currentTurn === 'X') {
currentTurn = 'O'
} else {
currentTurn = 'X'
}
}
}
</script>
<section class="gridbox">
<div class="box1" onclick="check(this, 0)"></div>
<div class="box1" onclick="check(this, 1)"></div>
<div class="box1" onclick="check(this, 2)"></div>
</section>
<!-- end snippet -->
答案2
得分: 0
这段代码主要是关于点击事件处理和类的使用。当点击一个框时,会检查它是否有一个名为"active"的类,如果没有,则添加这个类,如果已经有了,则不进行任何操作。这个示例中,当点击框时只是添加了一个背景颜色,你可以根据这个逻辑来实现你的井字棋游戏。
.grid { display: grid; grid-template-columns: 1fr 50px; width: 102px; gap: 2px; }
.box { width: 50px; aspect-ratio: 1; background-color: #efefef; border: 1px solid #666; }
.box:hover:not(.active) { cursor: pointer; background-color: lightblue; }
.active { background-color: lightskyblue; }
// 获取网格
const grid = document.querySelector('.grid');
// 给它附加一个事件监听器
// 这允许我们使用事件委托来捕获
// 其子元素的事件,因为它们在DOM中“冒泡”
grid.addEventListener('click', handleClick);
function handleClick(e) {
const el = e.target;
// 如果它是一个带有box类的元素
if (el.matches('.box')) {
// 如果元素没有一个active类,则添加一个
// 否则什么都不做
if (!el.classList.contains('active')) {
el.classList.add('active');
}
}
}
<section class="grid">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</section>
额外的文档:
英文:
It's less about whether the box can be clicked but more that nothing should happen if a box is clicked more than once. If you use classes you can check to see if a button has an "active" class (for example), and if it doesn't add an "active" class to it. If the code does have an active class nothing happens.
In this example I'm just adding a background colour to a boxes when it's clicked. But you can use this to implement your TTT logic.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Grab the grid
const grid = document.querySelector('.grid');
// Attach an event listener to it.
// This allows us to use event delegation to catch
// events from its children as they "bubble up" the DOM
grid.addEventListener('click', handleClick);
function handleClick(e) {
const el = e.target;
// If it's an element with a box class
if (el.matches('.box')) {
// If the element doesn't have an active class add one
// otherwise do nothing
if (!el.classList.contains('active')) {
el.classList.add('active');
}
}
}
<!-- language: lang-css -->
.grid { display: grid; grid-template-columns: 1fr 50px; width: 102px; gap: 2px; }
.box { width: 50px; aspect-ratio: 1; background-color: #efefef; border: 1px solid #666; }
.box:hover:not(.active) { cursor: pointer; background-color: lightblue; }
.active { background-color: lightskyblue; }
<!-- language: lang-html -->
<section class="grid">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</section>
<!-- end snippet -->
Additional documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
- grid
- javascript
- onclick
评论