Re: [問題] 關於Table 裡按按鍵的問題

看板Ajax作者 (煉)時間14年前 (2011/11/10 11:43), 編輯推噓0(009)
留言9則, 2人參與, 最新討論串2/2 (看更多)
: 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)

11/10 12:48, , 1F
似乎可以用這種寫法
11/10 12:48, 1F

11/10 12:48, , 2F
td.attachEvent("onmousedown", function(){SetBom(this);})
11/10 12:48, 2F

11/10 12:48, , 3F
那 請問我 SetBom(this) this 該打什麼呢>"<?
11/10 12:48, 3F

11/10 13:54, , 4F
我試了 this.event 可以抓到 左右鍵 可是抓不到 id
11/10 13:54, 4F

11/10 15:55, , 5F
var el = onevent.target || onevent.srcElement;
11/10 15:55, 5F

11/10 15:57, , 6F
onLeft(el); onRight(el);
11/10 15:57, 6F

11/10 16:00, , 7F
另外 微軟的左鍵是 1不是0 所以你的程式在IE左鍵也不會動
11/10 16:00, 7F

11/10 16:01, , 8F
上面第二行是 onLeft(el.id); onRight(el.id);
11/10 16:01, 8F

11/10 16:09, , 9F
太感謝 sk1765 大了...這問題困擾我一天了....感激...
11/10 16:09, 9F
文章代碼(AID): #1EkqXXtq (Ajax)
文章代碼(AID): #1EkqXXtq (Ajax)