为了账号安全,请及时绑定邮箱和手机立即绑定

棋盘点击不显示棋子?

棋盘点击不显示棋子?

装狐狸的兔子 2016-06-03 13:15:03
我的代码里面省略了背景图、省略了颜色渐变,但是点击鼠标事件触发不了?求解释页面代码为:<canvas id="chess" width="450px" height="450px"></canvas>js代码为:window.onload=function(){ drawchessboard(); drawchess(0,0,true); drawchess(1,1,false);}var a =document.getElementById('chess');var txt=a.getContext('2d');var drawchess=function(i,j,me){ if(me){ txt.fillStyle="#000000"; } else{ txt.fillStyle="#808080"; } txt.beginPath(); txt.arc(15+i*30,15+30*j,15,0,Math.PI*2,true); txt.closePath(); txt.fill();}var drawchessboard=function() { for(var i=0;i<15;i++) { txt.moveTo(15,15+i*30); txt.lineTo(435,15+i*30); txt.moveTo(15+i*30,15); txt.lineTo(15+i*30,435); txt.stroke(); }}chess.onclick=function (e) { var x=offsetX; var y=offsetY; var i=Math.floor(x/30); var j=Math.floor(y/30); drawchess(i,j,true); alert("hello");}
查看完整描述

1 回答

?
李夜

TA贡献35条经验 获得超31个赞

点击事件的对象 chess 应该是 document.getElementById('chess'); 

而你所获取对象的变量名是 a ,不是chess 。

查看完整回答
反对 回复 2016-06-19
  • 1 回答
  • 0 关注
  • 1215 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信