以前にも別の枠で似たような質問をさせていただいたのですが解決しなかったので再度質問させていただきます。
以下のプログラムで元の3×3のマスから4×4のマスすることはできたのですがボタンを押しても反応しなくなってしまいました。
どこがおかしいのかわかりません。
<script> try{ document.addEventListener ('click',function(e){myfunc(e)},true); }catch(e){ document.attachEvent('onclick',function(e){myfunc(e)}); } var cnt=0; function myfunc(e){ var t = (e.srcElement || e.target); if(t.nodeName=="INPUT" && t.type=="button"){ if(t.value==""){ cnt++; t.value=cnt%2==1?"○":"×"; if(cnt>=16){ alert("引き分け(OKでリセットします)"); resetFunc(t); cnt=0; } }else if( cnt > 2 ){ alert("そのマスはすでに選ばれています。"); } } if(t.nodeName=="INPUT" && t.type=="reset"){ cnt=0; resetFunc(t); } } function resetFunc(t){ var f=t.form; var a=new Array(); var j=0; for(var i=0;i<f.length;i++){ if(f[i].type=="button"){ a[j]=f[i].value; j++; } } if( (a[0]!=="" && a[0]==a[1] && a[0]==a[2] && a[0]==a[3])|||| (a[0]!=="" && a[0]==a[5] && a[0]==a[10] && a[0]==a[15])|| (a[0]!=="" && a[0]==a[4] && a[0]==a[8] && a[0]==a[12])||| (a[1]!=="" && a[1]==a[5] && a[1]==a[9] && a[1]==a[13])||| (a[2]!=="" && a[2]==a[6] && a[2]==a[10] && a[2]==a[14])|| (a[3]!=="" && a[3]==a[7] && a[3]==a[11] && a[3]==a[15])|| (a[3]!=="" && a[3]==a[6] && a[3]==a[9] && a[1]==a[12])||| (a[4]!=="" && a[4]==a[5] && a[4]==a[6] && a[4]==a[7])|||| (a[8]!=="" && a[8]==a[9] && a[8]==a[10] && a[8]==a[11])|| (a[12]!=="" && a[12]==a[13] && a[12]==a[14] && a[12]==a[15]){ var val=cnt%2==1?"○":"×"; alert(val+"の勝ち"); } } function resetFunc(t){ var f=t.form; for(var i=0;i<f.length;i++){ if(f[i].type=="button") f[i].value=""; } } </script> </head> <form name="tick"> <table> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> </table> <input type="reset" value="リセット"> </p> </form> </body>
<dateさんからのご指摘をいただいた部分を修正したものになります>
<script> try{ document.addEventListener ('click',function(e){myfunc(e)},true); }catch(e){ document.attachEvent('onclick',function(e){myfunc(e)}); } var cnt=0; function myfunc(e){ var t = (e.srcElement || e.target); if(t.nodeName=="INPUT" && t.type=="button"){ if(t.value==""){ cnt++; t.value=cnt%2==1?"○":"×"; if(cnt>=16){ alert("引き分け(OKでリセットします)"); resetFunc(t); cnt=0; } }else if( cnt > 2 ){ alert("そのマスはすでに選ばれています。"); } } if(t.nodeName=="INPUT" && t.type=="reset"){ cnt=0; resetFunc(t); } } function resetFunc(t){ var f=t.form; var a=new Array(); var j=0; for(var i=0;i<f.length;i++){ if(f[i].type=="button"){ a[j]=f[i].value; j++; } } if( (a[0]!=="" && a[0]==a[1] && a[0]==a[2] && a[0]==a[3])|| (a[0]!=="" && a[0]==a[5] && a[0]==a[10] && a[0]==a[15])|| (a[0]!=="" && a[0]==a[4] && a[0]==a[8] && a[0]==a[12])|| (a[1]!=="" && a[1]==a[5] && a[1]==a[9] && a[1]==a[13])|| (a[2]!=="" && a[2]==a[6] && a[2]==a[10] && a[2]==a[14])|| (a[3]!=="" && a[3]==a[7] && a[3]==a[11] && a[3]==a[15])|| (a[3]!=="" && a[3]==a[6] && a[3]==a[9] && a[1]==a[12])|| (a[4]!=="" && a[4]==a[5] && a[4]==a[6] && a[4]==a[7])|| (a[8]!=="" && a[8]==a[9] && a[8]==a[10] && a[8]==a[11])|| (a[12]!=="" && a[12]==a[13] && a[12]==a[14] && a[12]==a[15])){ var val=cnt%2==1?"○":"×"; alert(val+"の勝ち"); } } function resetFunc(t){ var f=t.form; for(var i=0;i<f.length;i++){ if(f[i].type=="button") f[i].value=""; } } </script> </head> <form name="tick"> <table> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> <tr> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> <td><input type="button"></td> </tr> </table> <input type="reset" value="リセット"> </p> </form> </body>
ご教授、ご指摘の方よろしくお願いします。
デベロッパーツール(http://www.buildinsider.net/web/chromedevtools/01)などで "JavaScript" のエラーを記述されては?ということなのですが。
なるほど。そういう意味ですか、わかりました。ありがとうございます。見てみます!
回答2件
あなたの回答
tips
プレビュー