如何更改 HTML 中图像按钮 onmouseover 事件上的图像?
我有 4 个包含图像的图像按钮。鼠标悬停时,我必须更改图像(即在按钮上加载新图像)。这是代码。
<div class ="submit" id="submit" >
<input type="image" src="dump.png" value="Dumb" class ="Dumb" id="Dumb" onclick="posting_by_user('Dumb')" />
<input type="image" src="informative.png" value="Informative" class ="Informative" id="Informative" onclick="posting_by_user('Informative')"/>
<input type="image" src="brilliant.png" value="Brilliant" class ="Brilliant" id="Brilliant" onclick="posting_by_user('Brilliant')"/>
<input type="image" src="cool.png" value="Cool" class ="Cool" id="Cool" onclick="posting_by_user('Cool')"/>
</div>
这里我加载了dump.png、informative.png、brittle.png 和cool.png。将鼠标悬停在每个按钮上时,我必须更改图像。
I have 4 image button containing a image. on mouse over i have to change the image(i.e load a new image on the button). Here is the code.
<div class ="submit" id="submit" >
<input type="image" src="dump.png" value="Dumb" class ="Dumb" id="Dumb" onclick="posting_by_user('Dumb')" />
<input type="image" src="informative.png" value="Informative" class ="Informative" id="Informative" onclick="posting_by_user('Informative')"/>
<input type="image" src="brilliant.png" value="Brilliant" class ="Brilliant" id="Brilliant" onclick="posting_by_user('Brilliant')"/>
<input type="image" src="cool.png" value="Cool" class ="Cool" id="Cool" onclick="posting_by_user('Cool')"/>
</div>
Here I have loaded dump.png, informative.png, brilliant.png and cool.png. on mouse over on each button i have to change the image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个
Try this
始终建议将代码与 html 分离。这个答案会很有帮助,因为它更干净并且有利于调试。
Segregating your code from html is always advised.. this answer will be helpful as it is cleaner and good for debugging..
您可以使用 Javascript 或 CSS 来实现此目的,下面是 javascript 方法。
JSFiddle 演示
请注意,在 HTML 中设置事件不是一个好的做法,最好用 Javascript 将它们联系起来。
CSS方式如下:
JS Fiddle Demo
You can use either Javascript or CSS for this, below is the javascript approach.
JSFiddle Demo
Note that setting events in HTML is not a good practice, it's better to tie them up in Javascript.
The CSS way is as follows:
JS Fiddle Demo