简体   繁体   English

悬停在另一个元素上时如何触发悬停

[英]How to trigger hover when hovering over another element

I am trying to trigger the hover css also when hovered over #boks2. 当我将鼠标悬停在#boks2上时,我也试图触发悬停CSS。

My jsfiddle: http://jsfiddle.net/u9byh/ 我的jsfiddle: http : //jsfiddle.net/u9byh/

My HTML: 我的HTML:

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

CSS: 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:

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

You can't trigger the CSS :hover from jQuery. 您无法从jQuery触发CSS :hover What you could do is to toggle a class on the other element. 您可以做的是在另一个元素上切换一个类。 Like this: 像这样:

CSS 的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 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.net/Wc7MC/ 您可以在这里查看http://jsfiddle.net/Wc7MC/

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

相关问题 jQuery-悬停在另一个元素上时触发悬停 - jquery - trigger hover on one element when hovering over another 悬停在另一个元素上时保持Li的悬停状态 - Keeping the Hover State of a Li When Hovering Over Another Element 如何在悬停另一个元素时显示元素并在悬停新元素时保持此悬停样式 - How to display element when hovering another element and maintain this hover styling when hovering the new element 如何在一个元素上触发 hover 而不实际悬停在它上面? - how to trigger hover on an element without actually hovering on it? jQuery / JavaScript-当鼠标悬停在另一个元素上时,触发悬停 - jQuery / JavaScript - Trigger hover on a element when mouse over on another element 将鼠标悬停在另一个元素上时触发.onclick() - Trigger .onclick() on hover over another element 鬼元素拖过来怎么触发hover? - How to trigger hover when dragging ghost element over? 将鼠标悬停在绝对定位的重叠元素上时,暂停悬停脚本 - Pausing hover script, when hovering over overlapping, absolutely positioned element 在悬停时隐藏背景图层,将鼠标悬停在上方时会收起 <p> 元件 - Hide background layer on hover, reapers when hovering over <p> element 仅在不将鼠标悬停在某些元素上时执行hover() - Only execute hover() when NOT hovering over certain element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM