gpt4 book ai didi

javascript - jquery click 不起作用,尽管我已经包含在 document.ready 中

转载 作者:行者123 更新时间:2023-11-28 03:11:39 25 4
gpt4 key购买 nike

jquery 点击不工作。虽然,这个问题已经讨论了很多,但我找不到能为我解决问题的答案。我已经在 $(document).ready 中包含了 jQuery 代码,但是徒劳无功。

这是html

<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="triliza01.css" type="text/css" />
<script type="text/javascript" src="jquery.1.11.2.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
$(document).ready(function() {

p1=new lPlayer("x.png");
p2=new lPlayer("o.jpg");
p1.setOponent(p2);
p2.setOponent(p1);
theWinner=play(p1);

}); // end of ready( ) function
</script>
</head>
<body>

<table>
<tr id="zRaw">
<td id="z0">

</td>
<td id="z1">

</td>
<td id="z2">
</td>
</tr>
<tr id="fRaw">
<td id="f0">
</td>
<td id="f1">
</td>
<td id="f2">
</td>
</tr>
<tr id="sRaw">
<td id="s0">
</td>
<td id="s1">
</td>
<td id="s2">
</td>
</tr>
</table>

</body>

这是javascript

function Player (sign) {
this.sign=sign;
this.oponent=null;
this.setOponent=function (player) {
this.oponent=player;
};
}

function lPlayer (sign) {
this.base=Player;
this.base(sign);

this.move=function (board) {

do {
for (var i in board) {
if (i.charAt(0)!=='#') continue;
//alert(board[i]);
$(i).click( function() {
if (board[i]==="none") {
board[i]=sign;
return i;
}
} );
}
} while (true);
};
}

lPlayer.prototype=new Player;


function Board() {
this["#z0"]="none";
this["#z1"]="none";
this["#z2"]="none";

this["#f0"]="none";
this["#f1"]="none";
this["#f2"]="none";

this["#s0"]="none";
this["#s1"]="none";
this["#s2"]="none";

this.full= function() {
cnt=0;
for (var i in this) {
if (this.hasOwnProperty(i)) //check it ??
if (this[i]==="none") cnt++;
}
return 9-cnt;
};

this.autoMove=function(sign) {
for (var i in this) {
if (this.hasOwnProperty(i))
if (this[i]==="none") {
this[i]=sign;
this.set(i,sign);
}
}
};

this.set=function(id, sign) {
$(id).css('background-image', 'url("'+sign+'")');
$(id).css('background-repeat', 'no-repeat');
$(id).css('background-size', '100% 100%');
};

this.win=function(player) {
s=player.sign;
if ((this["#z0"]===s && this["#z1"]===s && this["#z2"]===s) ||
(this["#f0"]===s && this["#f1"]===s && this["#f2"]===s) ||
(this["#s0"]===s && this["#s1"]===s && this["#s2"]===s) ||
(this["#z0"]===s && this["#f0"]===s && this["#s0"]===s) ||
(this["#z1"]===s && this["#f1"]===s && this["#s1"]===s) ||
(this["#z2"]===s && this["#f2"]===s && this["#s2"]===s) ||
(this["#z0"]===s && this["#f1"]===s && this["#s2"]===s) ||
(this["#z2"]===s && this["#f1"]===s && this["#s0"]===s)) return player;
return null;
};

this.winner=function(player) {
theWinner=this.win(player);
if (theWinner!==null) return theWinner;
return this.win(player.oponent);
};
}

function play(player) {
board=new Board();
theWinner=null;
current=player;
do {
var id;
if (board.full()===8) board.autoMove(current.sign);
else {
id=current.move(board);
board.set(id,current.sign);
}

current=current.oponent;
theWinner=board.winner(current);
} while ((theWinner===null) && (board.full()<9));
return theWinner;
}

问题是在 lPlayer 的方法 move 中我期待点击响应但它没有。

我也附上css文件

body {
position:fixed;
left:0px;
right: 0px;
bottom: 0px;
top:0px;
background-color:#666600;
}
td {
border: 1px solid darkslategray;

}

table {
width:100%;
height:100%;
background-color:#ff33cc;
}

欢迎任何帮助!谢谢!

最佳答案

您的代码中存在无限循环。不可能测试它。阅读你的代码,我认为它没有返回元素的正确 id,这就是为什么点击不起作用

关于javascript - jquery click 不起作用,尽管我已经包含在 document.ready 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30258796/

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