jquery单击不起作用。虽然这个问题已经讨论了很多,但我找不到一个能为我解决问题的答案。我已将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的方法移动中,我希望click响应,但它没有响应。
我还附上了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;
}
欢迎任何帮助!
谢谢您!
最佳答案
你的代码中有一个无限循环。无法测试。
阅读你的代码,我认为它没有返回元素的正确值,这就是为什么这个点击不起作用