1. 程式人生 > >js 三種事件的綁定

js 三種事件的綁定

() tag eve nod mouseup als listen src cell

第一種:前段常規綁定:

    <button type="button" class="recolor" onclick="reCol(‘rowIndex‘,‘colIndex‘)">重新著色</button>

第2種:js綁定監聽事件:

//添加監聽器
    table.addEventListener("click", display, false);  
} (generat());

//獲取鼠標點擊的行和列
function display(event) {
   
    var colIndex = null;
    var rowIndex = null
; var element = event.srcElement || event.target; if (!element) return; if (element.tagName != "TD") return; colIndex = element.cellIndex; rowIndex = element.parentNode.rowIndex; console.log("row:" + rowIndex + ", col:" + colIndex); return rowIndex, colIndex;
}

第2種:js+“”on“”綁定事件:

document.onmouseup = function (event) {
    console.log(event.target + onmouseup);
    tartgetTd = null;
    coltargetTd = null;
    resizeable = false;
    mousedown = false;
    colmousedown = false;
    colresizeable = false;
    document.body.style.cursor = ‘default‘;
}

js 三種事件的綁定