简体   繁体   English

表格单元格未触发onmouseover事件

[英]onmouseover event is not firing for a table cell

I've problem with onmouseevent for a table cell. 我对表格单元格的onmouseevent有问题。 What I'm doing is removing and creating html table cell with the help of jquery. 我正在做的是在jquery的帮助下删除并创建html表单元格。 When page loads this event fires perfectly fine. 页面加载时,此事件会正常触发。 But after removing and inserting table cell again at same position does not fire the onmouseover event. 但是,在相同位置再次删除并插入表格单元格后,不会触发onmouseover事件。 Below is the code what I've done... 下面是我完成的代码...

var ModularAdHolderCell = '';
var MergedCellValues = new Array();
$('#our_table tr').each(function (i, el) {
  for (var cellCnt = 0; cellCnt < this.cells.length; cellCnt++) {
    if ($(this.cells[cellCnt]).attr('class') == 'highlighted' || $(this.cells[cellCnt]).attr('class') == 'OrangeBackground highlighted') {
            var id = $(this.cells[cellCnt]).attr('id');
            ModularAdHolderCell = id;
            id = 'hdn_' + id;
            var MergedCells = $(this.cells[cellCnt]).find('input:hidden').val();
            if (MergedCells != '')
                MergedCellValues = MergedCells.trim().split('=');
        }
    }
});

var row = document.all.our_table.rows[0];
var TotalCellInRow = row.cells.length;
var Cell = row.insertCell(TotalCellInRow);
var element1 = document.createElement("input");
element1.type = "hidden";
element1.id = "hdn_" + MergedCellValues[cnt];

row.cells(TotalCellInRow).setAttribute('onmouseover', 'MOuseOver(this)');
row.cells(TotalCellInRow).setAttribute('onmouseout', 'MouseOut()');
row.cells(TotalCellInRow).setAttribute('onmousemove', 'MOuseOver(this)');
row.cells(TotalCellInRow).setAttribute('onmouseenter', 'MOuseOver(this)');

row.cells(TotalCellInRow).setAttribute('unitheight', Unitwidth);
row.cells(TotalCellInRow).setAttribute('unitwidth', UnitHeight);
row.cells(TotalCellInRow).setAttribute('id', MergedCellValues[cnt]);

row.cells(TotalCellInRow).setAttribute('width', Unitwidth);
row.cells(TotalCellInRow).setAttribute('height', UnitHeight);
row.cells(TotalCellInRow).appendChild(element1);

$(row).find('#' + MergedCellValues[cnt] + '').attr('onmouseover', 'MOuseOver(this)');
$(row).find('#' + MergedCellValues[cnt] + '').attr('onmouseout', 'MouseOut()');
$(row).find('#' + MergedCellValues[cnt] + '').attr('onmousemove', 'MOuseOver(this)');
$(row).find('#' + MergedCellValues[cnt] + '').attr('onmouseenter', 'MOuseOver(this)');

Here MergedCellValues is the array of cell id and above code is in the loop of the cell. 这里MergedCellValues是单元格ID的数组,上面的代码在单元格的循环中。

Can anyone tell why it does not firing onmouseover event for the cell ? 谁能说出为什么它不触发该单元的onmouseover事件?

Setting an attribute just isn't the intended way to attach an event handler, you can simplify and speed things up an great deal here, remove all of your .setAttribute('onmouseover', 'MOuseOver(this)'); 设置属性并不是附加事件处理程序的预期方式,您可以在此处简化和加快处理速度,删除所有.setAttribute('onmouseover', 'MOuseOver(this)'); logic...all 8 lines, then just attach one set of handlers to the <table> to handle all of this: 逻辑...所有8行,然后只需将组处理程序附加到<table>即可处理所有这些操作:

$("#our_table")
   .delegate("td", "mouseover mousemove mouseenter", MOuseOver)
   .delegate("td", "mouseout", MouseOut);

then in your MOuseOver and MouseOut functions, just use this to refer to the cell. 然后在您的MOuseOverMouseOut函数中,只需使用this来引用该单元格即可。

This attaches handlers to the <table> element to listen for the other mouse events bubbling up...no need for binding them per-cell, this is far less expensive and works on current and new cells combined. 这会将处理程序附加到<table>元素上,以侦听其他正在冒泡的鼠标事件...无需按单元绑定它们,这便宜得多,并且可以在当前和新单元上结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM