如何通过监听器引用数组中当前的 dom 元素来更改活动
目前,我正在循环遍历所有图像,并让它们的 onmouseover/onmouseout 事件通过 for 循环改变它们的图像。
var arrows = document.getElementsByClassName('arrow');
for (var i = 0; i < arrows.length; i++) {
arrows[i].onmouseover = arrows[i] + ".src = 'images/downarrow_hover.gif';";
arrows[i].onmouseout = arrows[i] + ".src = 'images/downarrow.gif';";
}
这不起作用,我这样做对吗?
Currently, I'm looping through all my images and making their onmouseover/onmouseout events change their image with a for-loop.
var arrows = document.getElementsByClassName('arrow');
for (var i = 0; i < arrows.length; i++) {
arrows[i].onmouseover = arrows[i] + ".src = 'images/downarrow_hover.gif';";
arrows[i].onmouseout = arrows[i] + ".src = 'images/downarrow.gif';";
}
It's not working, am I doing this right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将函数分配给
onmouseover
和onmouseout
。对于它的价值,如果你能负担得起,你应该研究 jQuery 这使得它轻而易举地做这样的事情:
You have to assign functions to
onmouseover
andonmouseout
.For what it's worth and if you can afford to, you should look into jQuery which makes it a breeze to do a such thing: