这里的功能非常简单,但无法正常工作。刚开始使用 Javascript,所以请对我温柔一点。也有人知道适合初学者的任何好的社区论坛。我觉得在这里问这个问题很简单,但也许不是。
<html>
<head>
<script type="text/javascript">
var img;
function mouseOver()
{
alert(img);
img.src ="button_over.jpg";
}
function mouseOut()
{
img.src ="button_out.jpg";
}
function init()
{
img = document.getElementById("buttonWrapper").getElementsByTagName('img')[0];
}
</script>
</head>
<body onLoad="javascript:init()">
<div id="buttonWrapper">
<img border="0" src="button_out.jpg" width="62" height="74" onmouseover="mouseOver()" onmouseout="mouseOut()" / >
</div>
</body>
</html>
最佳答案
现场演示: http://jsfiddle.net/jTB54/
只需将此代码放在页面底部(就在 </body>
之前),您将不需要加载处理程序:
var img = document.getElementById("buttonWrapper").getElementsByTagName('img')[0];
img.onmouseover = function() {
this.src = "button_over.jpg";
}
img.onmouseout = function() {
this.src = "button_out.jpg";
}
关于javascript 鼠标悬停代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4903241/