首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >javascript mouseOver代码

javascript mouseOver代码
EN

Stack Overflow用户
提问于 2011-02-05 05:41:42
回答 6查看 13.5K关注 0票数 1

这里的函数非常简单,但并不起作用。刚刚开始使用Javascript,所以请对我温柔点。另外,有没有人知道有什么好的社区论坛适合初学者。我觉得在这里问这个问题很简单,但也许不是。

代码语言: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>
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-02-05 05:44:58

帮你自己一个大忙--如果你刚刚开始使用Javascript,那就学习jQuery吧。它将极大地简化你正在尝试做的事情。转到here

在这种情况下,您可以使用示例here.轻松地将单击事件绑定到img标记

票数 -4
EN

Stack Overflow用户

发布于 2011-02-05 05:50:19

现场演示: http://jsfiddle.net/jTB54/

只需将此代码放在页面的底部(就在</body>之前),您就不需要onload处理程序:

代码语言:javascript
运行
复制
var img = document.getElementById("buttonWrapper").getElementsByTagName('img')[0];

img.onmouseover = function() {
    this.src = "button_over.jpg";
}

img.onmouseout = function() {
    this.src = "button_out.jpg";
}
票数 3
EN

Stack Overflow用户

发布于 2011-02-05 05:47:06

我不知道这是否能解决您的问题,但是这样做不是更容易吗?

代码语言:javascript
运行
复制
<html>
<head>
<script type="text/javascript">

function mouseOver(img)
{
    img.src ="button_over.jpg";
}
function mouseOut(img)
{
    img.src ="button_out.jpg";
}

</script>
</head>

<body>
    <div id="buttonWrapper">
        <img border="0" src="button_out.jpg" width="62" height="74" onmouseover="mouseOver(this)" onmouseout="mouseOut(this)" />
    </div>
</body>
</html>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4903241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档