首页 > 代码库 > js 三种事件的绑定

js 三种事件的绑定

第一种:前段常规绑定:

    <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 + onm ouseup);
    tartgetTd = null;
    coltargetTd = null;
    resizeable = false;
    mousedown = false;
    colmousedown = false;
    colresizeable = false;
    document.body.style.cursor = ‘default‘;
}

 

js 三种事件的绑定