JavaScript问题——onMouseOver事件
为什么这段代码没有按预期交换鼠标悬停时的图像?:
<a href="#" onMouseOver="
if (document.the_image.src == '01.jpg')
{
document.the_image.src = '02.jpg';
}
else if (document.the_image.src == '02.jpg')
{
document.the_image.src = '03.jpg';
}
else
{
document.the_image.src = '01.jpg';
}
">
Some image</a><br>
Why doesn't this piece of code swap images on mouse-over as intended?:
<a href="#" onMouseOver="
if (document.the_image.src == '01.jpg')
{
document.the_image.src = '02.jpg';
}
else if (document.the_image.src == '02.jpg')
{
document.the_image.src = '03.jpg';
}
else
{
document.the_image.src = '01.jpg';
}
">
Some image</a><br>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在渲染的 HTML 中,图像源很可能是绝对 URL,因此 src 可能是 "http://mydomain.com/01.jpg"
要测试这一点,请尝试在代码中设置 alert() 以查看实际情况src 值是
您可能还应该将该代码放入函数中,在内联 HTML 中放入大量 JavaScript。
Most likely in the rendered HTML, the image source is an absolute URL, so the src is probably "http://mydomain.com/01.jpg"
To test this, try setting an alert() in your code to see what the actual src value is
You should probably also put that code in a function, that's a lot of javascript to put in inline HTML.
为了补充 @jaywon 答案,如果是这种情况,您可以使用它来确保它匹配,无论绝对或相对 URL。
To complement @jaywon answer, if that is the case you can use this to ensure that it is matching regardless of absolute or relative URL.
最后,我弄清楚了如何发布完整的代码。非常感谢!:
Finally, I've figured out how to post the complete code. Thanks very much!: