gpt4 book ai didi

javascript - 函数内的函数未执行

转载 作者:行者123 更新时间:2023-12-02 22:43:49 24 4
gpt4 key购买 nike

抱歉,标题不好,我不知道如何正确表达它

这是我正在尝试构建的 Tic Tac Toe 游戏 -

我试图在reset()内部调用set(),以便每次单击#reset按钮时,下次单击容器时都会显示“X”。

现在,它适用于前两次单击,当我将函数传递到 HTML 按钮标记时,它会运行三次,但下一次单击会显示“O”。我每次都需要它工作,因为当你重置游戏时,应该是“玩家 1 的回合”,它在容器上显示“X”

#heads {
padding-top: 30px;
text-align: center;
font-size: 30px;
color: rgb(46, 46, 46);
}

#container {
text-align: center;
margin-top: 20px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
width: 300px;
height: 300px;
background-color: rgb(46, 46, 46);
border: 1px solid rgb(255, 255, 255);
}

#container div {
font-size: 25px;
border: 2px solid rgb(255, 255, 255);
color: white;
text-align: center;
padding-top: 35px;
}

#container div:hover {
background-color:rgb(58, 58, 58);
}

#reset {
margin-top: 10px;
background-color: rgb(46, 46, 46);
color: white;
border-radius: 3px;
outline: none;
}

#reset:hover {
background-color: rgb(58, 58, 58);
}

#divone {
font-size: 18px;
margin-top: 15px;
color: rgb(46, 46, 46);
}

#divtwo {
display: none;
font-size: 18px;
margin-top: 15px;
color: rgb(46, 46, 46);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="./style.css" type="text/css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <title>Tic Tac Toe </title>
</head>
<body>

<header id="heads">TIC TAC TOE</header>
<center>
<div id="divone">PLAYER 1's TURN</div>
<div id="divtwo">PLAYER 2's TURN</div>
</center>


<center>
<div id="container">


</div>
</center>

<center>
<button id="reset">RESET GAME</button>
</center>


</body>

<script>
const container = document.querySelector('#container')
gameBoard = ['', '', '', '', '', '', '', '', '']

// creates divs for gameboard
let html = '';
for (var i = 0; i <= 8; i++) {
html += '<div class="letters">' + gameBoard[i] + '</div>';
}
container.innerHTML += html;


// object constructors
function Player1(name) {
this.name = name
this.one = function () {
event.target.textContent = 'X'
let x = document.getElementById('playertwo')
}
}
function Player2(name) {
this.name = name
this.two = function () {
event.target.textContent = 'O'
}
}

let bop = new Player1('bop')
let bob = new Player2('bob')


// toggles between player 1 and player 2 functions to mark the table
function set() {
$('#container').click(function () {
var clicks = $(this).data('clicks');
if (clicks) {
bob.two()
} else {
bop.one()
}
$(this).data("clicks", !clicks);
});
}
set()


// 'reset game' button, clears table, sets 'player 1's turn', and makes the next click 'X'
function reset(){
document.querySelector('#reset').addEventListener("click", () => {


set()

container.innerHTML = html;

document.querySelector('#divone').style.display = 'block'
document.querySelector('#divtwo').style.display = 'none'

})
}
reset()


// display whos turn it is
container.addEventListener('click', () => {
if (document.getElementById('divone')) {

if (document.getElementById('divone').style.display == 'none') {
document.getElementById('divone').style.display = 'block';
document.getElementById('divtwo').style.display = 'none';
}
else {
document.getElementById('divone').style.display = 'none';
document.getElementById('divtwo').style.display = 'block';
}
}

})
</script>
</html>

感谢您的帮助!

最佳答案

问题出在 set 函数本身。当您不断调用 set 函数时,它会不断向容器添加多个事件监听器。因此,当您单击该框时,单击一次就会触发多个事件。相反,附加一个事件监听器一次,然后在重置时设置正确的数据属性,如下面的代码片段所示

#heads {
padding-top: 30px;
text-align: center;
font-size: 30px;
color: rgb(46, 46, 46);
}

#container {
text-align: center;
margin-top: 20px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
width: 300px;
height: 300px;
background-color: rgb(46, 46, 46);
border: 1px solid rgb(255, 255, 255);
}

#container div {
font-size: 25px;
border: 2px solid rgb(255, 255, 255);
color: white;
text-align: center;
padding-top: 35px;
}

#container div:hover {
background-color:rgb(58, 58, 58);
}

#reset {
margin-top: 10px;
background-color: rgb(46, 46, 46);
color: white;
border-radius: 3px;
outline: none;
}

#reset:hover {
background-color: rgb(58, 58, 58);
}

#divone {
font-size: 18px;
margin-top: 15px;
color: rgb(46, 46, 46);
}

#divtwo {
display: none;
font-size: 18px;
margin-top: 15px;
color: rgb(46, 46, 46);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="./style.css" type="text/css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <title>Tic Tac Toe </title>
</head>
<body>

<header id="heads">TIC TAC TOE</header>
<center>
<div id="divone">PLAYER 1's TURN</div>
<div id="divtwo">PLAYER 2's TURN</div>
</center>


<center>
<div id="container">


</div>
</center>

<center>
<button id="reset">RESET GAME</button>
</center>


</body>

<script>
const container = document.querySelector('#container')
gameBoard = ['', '', '', '', '', '', '', '', '']

// creates divs for gameboard
let html = '';
for (var i = 0; i <= 8; i++) {
html += '<div class="letters">' + gameBoard[i] + '</div>';
}
container.innerHTML += html;


// object constructors
function Player1(name) {
this.name = name
this.one = function () {
event.target.textContent = 'X'
let x = document.getElementById('playertwo')
}
}
function Player2(name) {
this.name = name
this.two = function () {
event.target.textContent = 'O'
}
}

let bop = new Player1('bop')
let bob = new Player2('bob')


// toggles between player 1 and player 2 functions to mark the table

$('#container').click(function () {
var clicks = $(this).data('clicks');
console.log(this, $(this).data("clicks"));
if (clicks) {
console.log('two called');
bob.two()
} else {
console.log('one called');
bop.one()
}
$(this).data("clicks", !clicks);
});




// 'reset game' button, clears table, sets 'player 1's turn', and makes the next click 'X'
function reset(){
document.querySelector('#reset').addEventListener("click", () => {


$('#container').data("clicks", false);

container.innerHTML = html;

document.querySelector('#divone').style.display = 'block'
document.querySelector('#divtwo').style.display = 'none'

})
}
reset()


// display whos turn it is
container.addEventListener('click', () => {
if (document.getElementById('divone')) {

if (document.getElementById('divone').style.display == 'none') {
document.getElementById('divone').style.display = 'block';
document.getElementById('divtwo').style.display = 'none';
}
else {
document.getElementById('divone').style.display = 'none';
document.getElementById('divtwo').style.display = 'block';
}
}

})
</script>
</html>

关于javascript - 函数内的函数未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58491862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com