jQuery选择器:如何:更改链接悬停时图像标签的src属性 [英] Jquery selector: How to: change the src attribute of an image tag on link hover

查看:85
本文介绍了jQuery选择器:如何:更改链接悬停时图像标签的src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当链接悬停在

<div class="clear span-33 last" id="navigation">
  <div class="hicon span-1"><a href="#" title="Homepage"><img src="../Assets/images/home.png" /></a></div>
</div>   

当链接未悬停在...上时,也将其更改为默认值...

Also change it to default when the link is not hovered on...

推荐答案

您确实应该研究使用CSS Sprite在悬停时切换背景.但是,如果您需要在jQuery中执行此操作,则应执行类似的操作.只需根据自己的喜好更改源代码上方的图片(还会预加载悬停图片):

You really should look into using CSS sprites for switching backgrounds on hover. But if you need to do this in jQuery, something like this should do it. Just change the over source image to your liking (also preloads the hover image):

var link = $('a'), 
    img  = link.children('img'), 
    orig = img.attr('src'), 
    over = 'over.png', 
    temp = new Image();

temp.src = over; // preloads

link.hover(function() {
    img.attr('src',over);
},function() {
    img.attr('src',orig);
}

这篇关于jQuery选择器:如何:更改链接悬停时图像标签的src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆