javascript - How to trigger hover when hovering over another element - Stack Overflow

I am trying to trigger the hover css also when hovered over #boks2. My jsfiddle: My HTML:<div id=&q

I am trying to trigger the hover css also when hovered over #boks2.

My jsfiddle: /

My HTML:

<div id="boks1"></div>
<div id="boks2"></div>

CSS:

#boks1 {width:50px;height:50px;background:blue;}
#boks1:hover {background:red;}
#boks2 {width:50px;height:50px;border:1px solid black;margin-top:10px;}

Jquery:

$(jQuery){
    $('#boks2').hover(function() {
        $('#boks1').hover();   
    });
});

I am trying to trigger the hover css also when hovered over #boks2.

My jsfiddle: http://jsfiddle/u9byh/

My HTML:

<div id="boks1"></div>
<div id="boks2"></div>

CSS:

#boks1 {width:50px;height:50px;background:blue;}
#boks1:hover {background:red;}
#boks2 {width:50px;height:50px;border:1px solid black;margin-top:10px;}

Jquery:

$(jQuery){
    $('#boks2').hover(function() {
        $('#boks1').hover();   
    });
});
Share Improve this question edited Dec 24, 2012 at 6:52 Yoav Kadosh 5,1954 gold badges44 silver badges58 bronze badges asked Oct 24, 2011 at 6:44 Rails beginnerRails beginner 14.5k35 gold badges140 silver badges259 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

http://jsfiddle/jordanbaucke/buyC9/3/

You can't trigger the CSS :hover from jQuery. What you could do is to toggle a class on the other element. Like this:

CSS

#boks1 {width:50px;height:50px;background:blue;}
#boks1.onHover, #boks1:hover {background:red;}
#boks2 {width:50px;height:50px;border:1px solid black;margin-top:10px;}

jQuery

$(function() { //on dom ready handler fixed as Sarfraz pointed out
    $('#boks2').hover(function() {
       $('#boks1').toggleClass('onHover');   
    });
 });
});
$('#boks2').hover(function() {
     $('#boks1').trigger('hover');   
});

$('#boks1').bind('hover',function(){
     $(this).css('background-color','red');
})

u can check here http://jsfiddle/Wc7MC/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744225248a4563972.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信