简体   繁体   English

更改图像onmouseover

[英]Change image onmouseover

What's the correct way to change an image on mouseover and back on mouseout (with/without jQuery)? 什么是鼠标悬停时更改图像的正确方法(使用/不使用jQuery)?

<a href="#" id="name">
    <img title="Hello" src="/ico/view.png" onmouseover="$(this).attr('src','/ico/view.hover.png')" />
</a>

Ok, this is working, but how to change back to the original image after mouseout ? 好的,这是有效的,但是如何在mouseout之后改回原始图像?

If it is possible, I want to do this thing inline, without document.ready function. 如果有可能,我想在没有document.ready函数的情况下内联这个东西。

here's a native javascript inline code to change image onmouseover & onmouseout: 这是一个原生的javascript内联代码来更改图像onmouseover&onmouseout:

<a href="#" id="name">
    <img title="Hello" src="/ico/view.png" onmouseover="this.src='/ico/view.hover.png'" onmouseout="this.src='/ico/view.png'" />
</a>

Try something like this: 尝试这样的事情:

HTML : HTML

<img src='/folder/image1.jpg' id='imageid'/>

jQuery: jQuery的:

$('#imageid').hover(function() {
  $(this).attr('src', '/folder/image2.jpg');
}, function() {
  $(this).attr('src', '/folder/image1.jpg');
});
  • DEMO DEMO

EDIT: (After OP HTML posted) 编辑:( OP HTML发布后)

HTML: HTML:

<a href="#" id="name">
    <img title="Hello" src="/ico/view.png"/>
</a>

jQuery: jQuery的:

$('#name img').hover(function() {
  $(this).attr('src', '/ico/view1.png');
}, function() {
  $(this).attr('src', '/ico/view.png');
});
  • DEMO DEMO

你要在/之前放一两个点

('src','./ico/view.hover.png')"

Here is an example: 这是一个例子:

HTML code: HTML代码:

<img id="myImg" src="http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif"/>

JavaScript code: JavaScript代码:

$(document).ready(function() {
    $( "#myImg" ).mouseover(function(){
        $(this).attr("src", "http://www.jqueryui.com/images/logo.gif");
    });

    $( "#myImg" ).mouseout(function(){
        $(this).attr("src", "http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif");
    });
});

Edit: Sorry, your code was a bit strange. 编辑:对不起,你的代码有点奇怪。 Now I understood what you were doing. 现在我明白你在做什么了。 ;) The hover method is better, of course. ;)当然,悬停方法更好。

jQuery has .mouseover() and .html() . jQuery有.mouseover().html() You can tie the mouseover event to a function: 您可以将mouseover事件绑定到一个函数:

  1. Hides the current image. 隐藏当前图像。
  2. Replaces the current html image with the one you want to toggle. 用您要切换的html图像替换当前的html图像。
  3. Shows the div that you hid. 显示您隐藏的div。

The same thing can be done when you get the mouseover event indicating that the cursor is no longer hanging over the div. 当你得到mouseover事件表明光标不再悬挂在div上时,可以做同样的事情。

You can do that just using CSS. 你可以使用CSS做到这一点。

You'll need to place another tag inside the <a> and then you can change the CSS background-image attribute on a:hover . 您需要在<a>放置另一个标记,然后您可以在a:hover上更改CSS background-image属性。

ie

HTML: HTML:

<a href="#" id="name">
  <span>&nbsp;</span> 
</a>

CSS: CSS:

a#name span{
  background-image:url(image/path);
}

a#name:hover span{
  background-image:url(another/image/path);
}
<a href="" onMouseOver="document.MyImage.src='http://icons.iconarchive.com/icons/uiconstock/round-edge-social/72/ask-icon.png';" onMouseOut="document.MyImage.src='http://icons.iconarchive.com/icons/uiconstock/round-edge-social/72/arto-icon.png';">
<img src="http://icons.iconarchive.com/icons/uiconstock/round-edge-social/72/arto-icon.png" name="MyImage">

Demo http://jsfiddle.net/W6zs5/ 演示http://jsfiddle.net/W6zs5/

I know someone answered this the same way, but I made my own research, and I wrote this before to see that answer. 我知道有人以同样的方式回答了这个问题,但是我做了自己的研究,之前我写过这篇文章来看看答案。 So: I was looking for something simple with inline JavaScript, with just on the img , without "wrapping" it into the a tag (so instead of the document.MyImage , I used this.src ) 所以:我正在寻找一些简单的内联JavaScript,只需要在img ,而不是将它“包装”到a标签中(所以不使用document.MyImage ,我使用了this.src

<img 
onMouseOver="this.src='ico/view.hover.png';" 
onMouseOut="this.src='ico/view.png';" 
src="ico/view.png" alt="hover effect" />

It works on all currently updated browsers; 它适用于所有当前更新的浏览器; IE 11 (and I also tested it in the Developer Tools of IE from IE5 and above), Chrome, Firefox, Opera, Edge. IE 11(我还在IE5及更高版本的IE开发者工具中测试过它),Chrome,Firefox,Opera,Edge。

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

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