DOM onmouseover 未出现

发布于 2024-12-12 06:50:47 字数 316 浏览 1 评论 0原文

我试图在表中创建新行时插入 onmouseover,但它没有出现。我错过了一些愚蠢的事情吗?

    var row = document.createElement("TR");
    row.id = i;
    row.onmouseover = hover(i);
    var td1 = document.createElement("TD");
    row.appendChild(td1);
    tbody.appendChild(row);

变量“i”是循环中的当前数字。该行的 ID 看起来不错,但鼠标悬停时却不然。

I'm trying to insert an onmouseover when creating new rows within my table however it's not appearing. Am I missing something stupid?

    var row = document.createElement("TR");
    row.id = i;
    row.onmouseover = hover(i);
    var td1 = document.createElement("TD");
    row.appendChild(td1);
    tbody.appendChild(row);

The variable 'i' is the current number in the loop. The ID of the row appears fine, but not the onmouseover.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-12-19 06:50:47

使用匿名函数为 i 的值创建闭包,并确保将函数设置为 onmouseover,而不是调用函数的结果:

var row = document.createElement("TR");
(function (i) {
    row.onmouseover = function () { hover(i) };
})(row.id);
var td1 = document.createElement("TD");
row.appendChild(td1);
tbody.appendChild(row);


Taking a proper look at your code, it appears that you're not actually setting the id attribute of the TR element. However, you might want to avoid that entirely and use this context inside the hover function:

var row = document.createElement("TR");
row.onmouseover = hover;
var td1 = document.createElement("TD");
row.appendChild(td1);
tbody.appendChild(row);
function hover() { 
    alert(this.rowIndex);  // <-- `this` refers to the row element
}

Use an anonymous function to create a closure for the value of i, and make sure you're setting a function to onmouseover, rather than the result of calling a function:

var row = document.createElement("TR");
(function (i) {
    row.onmouseover = function () { hover(i) };
})(row.id);
var td1 = document.createElement("TD");
row.appendChild(td1);
tbody.appendChild(row);


Taking a proper look at your code, it appears that you're not actually setting the id attribute of the TR element. However, you might want to avoid that entirely and use this context inside the hover function:

var row = document.createElement("TR");
row.onmouseover = hover;
var td1 = document.createElement("TD");
row.appendChild(td1);
tbody.appendChild(row);
function hover() { 
    alert(this.rowIndex);  // <-- `this` refers to the row element
}
相权↑美人 2024-12-19 06:50:47

您正在将函数的结果分配给事件。

需要类似于

row.onmouseover=function(){hover(this);}

最好使用它,因为您拥有 DOM 对象并且不必查找任何内容。

function hover( row ){
     row.style.color = "red";
}

如果您仍然要走我的路,您需要更改您的 ID,使其有效。 Id 不能以数字开头。

var row = document.createElement("TR");
row.id = "row_i";
row.onmouseover = function(){ hover(this.id); }
var td1 = document.createElement("TD");
row.appendChild(td1);
tbody.appendChild(row);

You are assigning the result of the function to the event.

Needs to be something like

row.onmouseover=function(){hover(this);}

And it is better to use this since you have the DOM object and do not have to look up anything.

function hover( row ){
     row.style.color = "red";
}

If you still what to go the i way, you need to change your id so it is valid. Ids can not start with a number.

var row = document.createElement("TR");
row.id = "row_i";
row.onmouseover = function(){ hover(this.id); }
var td1 = document.createElement("TD");
row.appendChild(td1);
tbody.appendChild(row);
怀中猫帐中妖 2024-12-19 06:50:47

也许尝试:

row.onmouseover = function() { hover(i); };

Maybe try:

row.onmouseover = function() { hover(i); };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文