DOM onmouseover 未出现
我试图在表中创建新行时插入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用匿名函数为
i
的值创建闭包,并确保将函数设置为onmouseover
,而不是调用函数的结果: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 usethis
context inside the hover function:Use an anonymous function to create a closure for the value of
i
, and make sure you're setting a function toonmouseover
, rather than the result of calling a function: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 usethis
context inside the hover function:您正在将函数的结果分配给事件。
需要类似于
最好使用它,因为您拥有 DOM 对象并且不必查找任何内容。
如果您仍然要走我的路,您需要更改您的 ID,使其有效。 Id 不能以数字开头。
You are assigning the result of the function to the event.
Needs to be something like
And it is better to use this since you have the DOM object and do not have to look up anything.
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.
也许尝试:
Maybe try: