javascript - 在运行时将事件处理程序附加到每个表格单元格

标签 javascript jquery

我在设计时创建了一个表,并在运行时向其中添加行。 我想将鼠标悬停事件附加到显示相应工具提示的第一列的每一行。

for(ctr=0;ctr<noOfRows;ctr++){
   var myTable=document.getElementById("myTable");
   var newRow = myTable.insertRow(1);

   var cell0 = newRow.insertCell(0);
   cell0.innerHTML="Cell Data"+"<div class='hiddenToolTip' id='tip"+ctr+"'>"+tooltip+"</div>";

   cell0.onmouseover=function(){
        $("#tip"+ctr).show('blind',500);
   };

   cell0.onmouseout=function(){
        $("#tip"+ctr).hide();
   };
}

问题是“ctr”变量总是作为“onmouseover”和“onmouseout”函数中的最高值出现。

最佳答案

只需在函数参数中传递 this 并使用元素的 id 获取 ctrl 的值,如下所示

cell0.onmouseover=function()
{      
    $(this).children("div[id^='tip'] ").show('blind',500);    
    };

为此检查此示例:Javascript: Adding OnMouseOver And OnMouseOut Using DOM

关于javascript - 在运行时将事件处理程序附加到每个表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10121467/

相关文章:

javascript - 如何在 Web View 页面上从本地存储调用 CSS 和 JS URL

javascript 转到herf并滚动到元素

javascript - 如果选项文本包含 Select,则选择框验证

javascript - JSON,数组推送功能不适用于变量定义的键

jquery - 如何让 CSS 动画 DIV 开始隐藏并逐一显示?

javascript - 如何解决这个 MongoDB/Node 异步问题?

javascript - javascript中的简单货币转换

javascript - 使用 D3.js 计算 SVG 路径质心

javascript - 如何使用jquery获取行字段(嵌套对象)的值

javascript - 模式打开时保持背景滚动