简体   繁体   中英

When hover a text, change the background of other div using jquery

When hover the link I am trying to change the background of previous div, But here it is not working here is code Jsfiddle this is my script

$('div a.link').hover(function(){

var icon = $(this).prevAll('div.icon').first();          
          icon.css({'background':'url(http://www.rangde.org/images/icons/circle_grey_hover.jpg) no-repeat', 'color':'#fff','cursor':'pointer'});

 });          

my html

<div class="icon">1</div>
<div><a href="#" class="link">First article</a></div>
<div class="clearfloat"></div>
<div class="icon">2</div>
<div><a href="#" class="link">Second article</a></div>
<div class="clearfloat"></div>
<div class="icon">3</div>
<div><a href="#" class="link">Third article</a></div>

my css

.icon{background: url(http://www.rangde.org/images/icons/circle_grey.jpg) no-repeat;
width: 92px;
height: 97px;
padding: 35px 0px 0px 45px;
font-size: 14pt;
color: black;
font-weight: 600;
width: 32%;float:left;}
.link{width:60%;float:left;color:#048ccc;font-weight:600;padding-top:30px;}
.clearfloat{clear:both;}
​
 $('div a.link').hover(function(){
     var icon = $(this).parent().prev();          
     icon.css({'background':'url(http://www.rangde.org/images/icons/circle_grey_hover.jpg) no-repeat', 'color':'#fff','cursor':'pointer'});
 }, function() {
     var icon = $(this).parent().prev().css({'background':'none'});
 });           

​

http://jsfiddle.net/FEdk9/9/

Why use jQuery when you can do this with just css? http://jsfiddle.net/czW5d/

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