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