javascript - jquery click does not work although I've enclosed in document.ready -
jquery click not working. although, issue has been discussed lot, can not find answer solves problem me. have enclosed jquery code in $(document).ready vainly.
here 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>
and here 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) { { (var 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; (var in this) { if (this.hasownproperty(i)) //check ?? if (this[i]==="none") cnt++; } return 9-cnt; }; this.automove=function(sign) { (var 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; { 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; }
the problem in method move of lplayer expecting click respond not.
i enclose css file
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; }
any welcomed! thank you!
there infinite loop in code. impossible test it. , reading code, think doesn't return right id
of element, why click doesn't work
Comments
Post a Comment