问:正如标题所说,我想有一个图像(mocha1150.Gif),当对他们ID鼠标移动像他们那样改变mocha5150ani.Gif当鼠标移出像ID改回mocha1150.Gif
我已经编写了代码,有人可以告诉我这是怎么回事吗?
展开| 选择| 包装| 行号
<html>
<头>
<script type =“ text / javascript”>
功能转移(imga,ida)
{
document.getElementById('ida')。src =“ imga”;
}
函数moveout(imgb,idb)
{
document.getElementById('idb')。src =“ imgb”;
}
</ script>
</ head>
<div style =“ position:absolute; top:20px; left:20%” align =“ center”> <font face =“ arial” id =“ title” size =“ 20”>欢迎使用Spencer的测试网站</ font > </ div>
<img src =“ top.GIF” width =“ 100%” height =“ 75px” />
<br>
<img id =“ aa” src =“ mocha1150.GIF”
onmouseover =“ moveover(mocha5150ani.GIF,aa)”
onmouseout =“ moveout(mocha1150.GIF,aa)” />
<html>
另外,如果他们有一种无需创建函数就可以在元素内完成该操作的方法,有人可以告诉我如何吗?
答:首先,函数使编码更容易。无需丢弃它们。
第二,在JavaScript中不引用变量。也就是说,在函数中定义了2个参数,但没有使用它们。与此相反,必须始终对字符串加引号。
第三,您拥有2个功能完全相同的功能……
第四,(高级)您不需要document.getElementById()调用。为当前元素使用关键字更简洁。
展开| 选择| 包装| 行号
函数changeSource(pic,obj)
{
obj.src =图片;
}
展开| 选择| 包装| 行号
<img src =“ mocha1150.GIF” onmouseover =“ changeSource('mocha5150ani.GIF',this)” onmouseout =“ changeSource('mocha1150.GIF',this)” />
PS。即使可以简化,也要稍后。