从一系列 onMouseOver 事件创建循环
我怎样才能从这个函数中创建一个循环:
window.onload = function makeHalo() {
document.getElementById("d1").onmouseover = function() {
this.id ="d1On";
this.className="hover";
document.getElementById("menu1").style.color="#6DC5E6";
};
document.getElementById("menu1").onmouseover = function() {
this.style.color="#6DC5E6";
document.getElementById("d1").className="hover";
document.getElementById("d1").id="d1On";
};
document.getElementById("d1").onmouseout = function() {
this.id ="d1";
this.className="";
document.getElementById("menu1").style.color="#FFFFFF";
};
document.getElementById("menu1").onmouseout = function() {
this.style.color="#FFFFFF";
document.getElementById("d1On").className="";
document.getElementById("d1On").id="d1";
};
document.getElementById("d2").onmouseover = function() {
this.id ="d2On";
this.className="hover";
document.getElementById("menu2").style.color="#6DC5E6";
};
document.getElementById("menu2").onmouseover = function() {
this.style.color="#6DC5E6";
document.getElementById("d2").className="hover";
document.getElementById("d2").id="d2On";
};
document.getElementById("d2").onmouseout = function() {
this.id ="d2";
this.className="";
document.getElementById("menu2").style.color="#FFFFFF";
};
document.getElementById("menu2").onmouseout = function() {
this.style.color="#FFFFFF";
document.getElementById("d2On").className="";
document.getElementById("d2On").id="d2";
};
}
该函数几乎可以在图像悬停时学习图像的ID,更改该元素的ID,向该元素添加一个类,并更改另一个元素的颜色
第二部分学习列表项悬停时的 ID,更改其颜色,更改其他图像元素的 ID,并向该元素添加一个类。
onmouseout
只是重置一切。
在实际的 HTML 页面上,它是一个带有列表的菜单页面。下面有一张大陆地图,这是背景图片。当您将鼠标悬停在列表项上时,它会将地图上的点替换为另一张图片作为指示器。您还可以将鼠标悬停在地图上的点来更改列表上链接的颜色。
我尝试做类似的事情,但循环仅转到某些元素的最后一次迭代。链接的颜色变化很好,但无论我将鼠标悬停在哪个链接上,它都只会将图片交换为“d43”。
window.onload = function makeHalo() {
var i = 1;
for (i=1; i<44; i++) {
var menu = "menu"+i;
var d = "d"+i;
var On = "d"+i+"On";
document.getElementById(d).onmouseover = function() {
this.id = On;
this.className="hover";
document.getElementById(menu).style.color="#6DC5E6";
};
document.getElementById(menu).onmouseover = function() {
this.style.color="#6DC5E6";
document.getElementById(d).className="hover";
document.getElementById(d).id=On;
};
document.getElementById(d).onmouseout = function() {
this.id = d;
this.className="";
document.getElementById(menu).style.color="#FFFFFF";
};
document.getElementById(menu).onmouseout = function() {
this.style.color="#FFFFFF";
document.getElementById(On).className="";
document.getElementById(On).id=d;
};
}
}
任何帮助将不胜感激。
How can I create a loop out of this function:
window.onload = function makeHalo() {
document.getElementById("d1").onmouseover = function() {
this.id ="d1On";
this.className="hover";
document.getElementById("menu1").style.color="#6DC5E6";
};
document.getElementById("menu1").onmouseover = function() {
this.style.color="#6DC5E6";
document.getElementById("d1").className="hover";
document.getElementById("d1").id="d1On";
};
document.getElementById("d1").onmouseout = function() {
this.id ="d1";
this.className="";
document.getElementById("menu1").style.color="#FFFFFF";
};
document.getElementById("menu1").onmouseout = function() {
this.style.color="#FFFFFF";
document.getElementById("d1On").className="";
document.getElementById("d1On").id="d1";
};
document.getElementById("d2").onmouseover = function() {
this.id ="d2On";
this.className="hover";
document.getElementById("menu2").style.color="#6DC5E6";
};
document.getElementById("menu2").onmouseover = function() {
this.style.color="#6DC5E6";
document.getElementById("d2").className="hover";
document.getElementById("d2").id="d2On";
};
document.getElementById("d2").onmouseout = function() {
this.id ="d2";
this.className="";
document.getElementById("menu2").style.color="#FFFFFF";
};
document.getElementById("menu2").onmouseout = function() {
this.style.color="#FFFFFF";
document.getElementById("d2On").className="";
document.getElementById("d2On").id="d2";
};
}
The function pretty much learns the ID of an image when its hovered, changes the ID of that element, adds a class to the element, and changes the color of another element
The second part learns the ID of a list item when its hovered, changes its color, and changes the ID of the other image element and adds a class to that element as well.
The onmouseout
simply resets everything.
On the actual HTML page, it is a menu page with lists. Below there a continent map, which is a background image. When you hover over a list item, it swaps out a point on a map with another picture for an indicator. You can also hover the points on the map to change the color of the links on the lists.
I tried doing something like this, but the loop only goes to the last iteration for some of the elements. The links change color fine, but it will only swap the picture for "d43" regardless of what link I hover over.
window.onload = function makeHalo() {
var i = 1;
for (i=1; i<44; i++) {
var menu = "menu"+i;
var d = "d"+i;
var On = "d"+i+"On";
document.getElementById(d).onmouseover = function() {
this.id = On;
this.className="hover";
document.getElementById(menu).style.color="#6DC5E6";
};
document.getElementById(menu).onmouseover = function() {
this.style.color="#6DC5E6";
document.getElementById(d).className="hover";
document.getElementById(d).id=On;
};
document.getElementById(d).onmouseout = function() {
this.id = d;
this.className="";
document.getElementById(menu).style.color="#FFFFFF";
};
document.getElementById(menu).onmouseout = function() {
this.style.color="#FFFFFF";
document.getElementById(On).className="";
document.getElementById(On).id=d;
};
}
}
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您面临的主要技术问题是您正在循环中创建闭包。这些回调中的每一个都通过相同
i
变量结束,该变量的值对于每个回调都是相同的(最终迭代后的值)。这是通过将循环体包装在其自己的函数中来解决的,该函数接收i
作为参数,从而在每次迭代时创建一个本地副本。还存在许多样式和性能问题:
mouseover
和mouseout
对最终结果相同在每个区块中工作)。我会写得更像这样(解决关闭问题和上面的前两个项目符号项目(不解决 ID 问题)):
这仅处理两个元素;只需更改循环最大值即可使其工作更多。
您可以在此处查看工作演示:
The primary technical issue you're facing is that you're creating closures in a loop. Each one of those callbacks closes over the same
i
variable, whose value will be the same for each of the callbacks (its value after the final iteration). This is fixed by wrapping the body of the loop in its own function that receivesi
as an argument, thus creating a local copy on each iteration.There are a number of style and performance issues, as well:
mouseover
andmouseout
pairs end up dong the same work in each block).I would write it more like this (addressing the closure issue and the first two bullet items above (not addressing the ID problem)):
This handles just two elements; simply change the loop max to make it work for more.
You can see a working demo here:
HTML 中的最后一个 div 是“d43”还是“d44”?您的循环将运行从 d1 到 d43,因为 i<44,这意味着当 i 为 44 时,它将退出循环,因此它将在 d43 处停止。
如果您希望它到达 d44,则将条件更改为: i <= 44
或者将其更改为 i < 45
顺便说一句,您不使用 jQuery 是有原因的,它的设计是通过多种方式使此类事情变得更加容易。也许您列出了您实际上试图使用此代码完成的任务,例如它是菜单系统还是我们可能能够建议更好的方法的东西。
Is your last div in your HTML "d43" or is it "d44"? Your loop will run through d1 through d43 because you have i<44 which means when i is 44 it will exit the loop so it will stop at d43.
If you want it to get to d44, then either change the condition to: i <= 44
or change it to i < 45
By the way is there is reason you are not using jQuery it's design to make things like this much easier, in several ways. Maybe you listed what you were actually trying to accomplish with this code for example whether it's a menu system or something we might be able to suggest better approaches.
这里不需要 JavaScript...只需使用 CSS
:hover
伪类。但是,回答你的问题:
i
的值为45
,对于 所有元素。通过将i
传递给创建事件处理程序的函数来解决此问题:No need for JavaScript here... just use the CSS
:hover
pseudo-class.But, to answer your question:
i
is45
, for all elements. Solve this by passingi
to a function that creates your event handlers: