作者kiey (炼)
看板Ajax
标题Re: [问题] 关於Table 里按按键的问题
时间Thu Nov 10 11:43:26 2011
: function CreatTable(){
: for( var i=0;i<sizeArray[1];i++) {
: tr = bomBody.insertRow(bomBody.rows.length);
: for( var j=0;j<sizeArray[0];j++) {
: td = tr.insertCell(tr.cells.length);
: td.id=('x'+j+'y'+i);
: td.innerHTML = ' ';
: if ( td.addEventListener )
: td.addEventListener("mousedown", SetBom, false);
: else
: td.setAttribute("onmousedown", SetBom);
: } } }
: function SetBom(onevent) {
: var onevent = onevent || window.event;
: var onLeftButton=0 , onRightButton=2 ,
: onMouseButton = onevent.button;
: //alert(onMouseButton);
: if ( onMouseButton == onLeftButton )
: onLeft(this.id);
: else if( onMouseButton == onRightButton)
: onRight(this.id);
: }
小弟本来先写其他功能、写完回来看发现...只要把
td.setAttribute("onmousedown", SetBom);
改成
td.attachEvent("onmousedown", SetBom);
就可以了
但是现在又有一个问题就是 在 SetBom(onevent) 收不到传过来的 id
不知道是哪里用错了...还是 IE 不支援这种写法呢? 如果是 该如何改呢?
--
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 1.160.239.183
※ 编辑: kiey 来自: 1.160.239.183 (11/10 11:43)
※ 编辑: kiey 来自: 1.160.239.183 (11/10 11:44)
1F:→ kiey:似乎可以用这种写法 11/10 12:48
2F:→ kiey:td.attachEvent("onmousedown", function(){SetBom(this);}) 11/10 12:48
3F:→ kiey:那 请问我 SetBom(this) this 该打什麽呢>"<? 11/10 12:48
4F:→ kiey:我试了 this.event 可以抓到 左右键 可是抓不到 id 11/10 13:54
5F:→ sk1765: var el = onevent.target || onevent.srcElement; 11/10 15:55
6F:→ sk1765:onLeft(el); onRight(el); 11/10 15:57
7F:→ sk1765:另外 微软的左键是 1不是0 所以你的程式在IE左键也不会动 11/10 16:00
8F:→ sk1765:上面第二行是 onLeft(el.id); onRight(el.id); 11/10 16:01
9F:→ kiey:太感谢 sk1765 大了...这问题困扰我一天了....感激... 11/10 16:09