JavaScript问题——onMouseOver事件

JavaScript问题——onMouseOver事件,javascript,onmouseover,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_ima

为什么这段代码不能按预期在鼠标上交换图像

<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>


在呈现的HTML中,图像源很可能是一个绝对URL,因此src可能是

要测试这一点,请尝试在代码中设置一个警报(),以查看实际的src


您可能还应该将该代码放入函数中,这需要在内联HTML中放入大量javascript

为了补充@jaywon answer,如果是这种情况,您可以使用它来确保它是匹配的,而不管绝对或相对URL是什么

if (document.the_image.src.indexOf('01.jpg') > 0) {
...
}

最后,我知道了如何发布完整的代码。非常感谢

<HTML>
<head>

<title></title>
<script language="javascript">
    var name = prompt('What is your name?', '');
    document.writeln('Welcome, ' + name + '.');
</script>
</head>


<body>

<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';
    }
    ">
<img src="01.jpg" name="the_image"></a><br>


</body>

</HTML>

var name=prompt('你叫什么名字?','');
document.writeln('Welcome',+name+');


文档对象的图像属性是什么?Jonathan,我试图发布完整的代码,但我一直从网站上收到以下消息:“哎呀!无法提交您的问题,因为:很抱歉,作为垃圾邮件预防机制,新用户不允许发布图像。获得10个声誉来发布图片。“+1用于解决方案和在夏威夷工作。我希望我在那里:-)jaywon,你的评论对我很有意义。非常感谢。