简体   繁体   中英

Changing image source attribute on mouse hover

I have a requirement where I want to change source of image on mouse hover. The way I am doing this is:

document.getElementsByTagName('img').addEventListener('mouseover', function() 
{
    document.getElementsByTagName('img').setAttribute('src', 'url/of/the/image');
});

I want to know is this correct way of doing this. Or should add an overlay div on image tag and show my image there?

With jQuery you can do it like this:

$('img').on('mouseover', function(e) {
   $(this).attr('src', '/link/to/image').css({}); // change src and css
   // or you can use div with background image style property
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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