简体   繁体   English

如何在HTML中的图像按钮onmouseover事件上更改图像?

[英]how to change the image on a image button onmouseover event in HTML?

I have 4 image button containing a image. 我有4个包含图像的图像按钮。 on mouse over i have to change the image(ie 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. 我在这里加载了dump.png,informative.png,brilliant.png和cool.png。 on mouse over on each button i have to change the image. 在鼠标悬停在每个按钮上,我必须更改图像。

<input  type="image" src="dump.png" value="Dumb" class ="Dumb" id="Dumb" onclick="posting_by_user('Dumb')" onmouseover="changeImg.call(this)" />

function changeImg(){
this.setAttribute("src","newImageFileName.jpg");
}

Segregating your code from html is always advised.. this answer will be helpful as it is cleaner and good for debugging.. 始终建议您将代码与html隔离。此答案将更为有用,因为它更干净而且调试也很好。

<input  type="image" src="dump.png" value="Dumb" class ="Dumb" id="Dumb" 
onclick="posting_by_user('Dumb')" 
onmouseover="this.src='informative.png';" 
onmouseout="this.src='dump.png';" />

Try this 尝试这个

$(document).ready(function() {
$('input[type=img]')
    .mouseover(function() { 
        var src =  "mouseover.png";
        $(this).attr("src", src);
    });

You can use either Javascript or CSS for this, below is the javascript approach. 您可以为此使用Javascript或CSS,以下是javascript方法。

window.addEventListener("load", function load (event) {
    window.removeEventListener("load", load, false);
    tieEvents();
}, false);

function tieEvents() {
    var button = document.getElementById('Dumb');

    button.addEventListener("mouseover", hovered, false);
    button.addEventListener("mouseout", unhovered, false);

    function hovered() {
        button.src = "anotherimg.png";
    }

    function unhovered() {
        button.src = "anotherimg.png";
    }
};

JSFiddle Demo

Note that setting events in HTML is not a good practice, it's better to tie them up in Javascript. 请注意,在HTML中设置事件不是一个好习惯,最好将它们与Javascript绑定在一起。


The CSS way is as follows: CSS方式如下:

#Dumb {
    backgroud: url("dump.png");
}

#Dumb:hover {
    backgroud: url("another.png");
}

JS Fiddle Demo

you can use CSS 你可以使用CSS

  1. rule will display default look 规则将显示默认外观
  2. on mouse hover shows another image in same size basic example 在鼠标悬停时显示相同大小的另一个图像基本示例

    button1 {background: url(img/button1.png)} button1 {背景:url(img / button1.png)}
    button1:hover {background: url(img/button1_hover.png)} button1:悬停{background:url(img / button1_hover.png)}

or you can use sprite image, have both in one image and use CSS rules to change its position 或者您也可以使用精灵图片,将图片同时包含在其中,并使用CSS规则更改其位置

http://cssglobe.com/post/3028/creating-easy-and-useful-css-sprites http://cssglobe.com/post/3028/creating-easy-and-useful-css-sprites

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM