简体   繁体   English

如何在用JQuery悬停另一个元素时隐式悬停两个元素?

[英]How to hover two elements implicitly while hovering another one with JQuery?

is there a way to trigger the hover of two elements when hovering on another one with JQuery? 用JQuery悬停在另一个元素上时,是否有办法触发两个元素的悬停?

Here is my HTML: 这是我的HTML:

<!DOCTYPE HTML>
   <html lang="">
    <head>
      <meta http-equiv="Content-Type" content="text/html"/>
      <meta charset="utf-8"/>
          <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
          <script type='text/javascript' src='javascript.js'></script>
          <link rel='stylesheet' href='style.css' type='text/css'>
      <title>Title</title>
   </head>
   <body>
       <div class='hidden'></div>
       <div class='triggerOtherHover' onmouseover='hoverImplicitly();'></div>
       <div class='hidden'></div>
   </body>
  </html> 

Here is my style.css: 这是我的style.css:

.hidden { 
          background-color:red;
          border: 1px solid black;
          opacity:0;
          transition: opacity 0.4s ease-in-out;
          width: 100px;
          height:100px;
 }
.hidden:hover, .visible {
          opacity:1;
}

.triggerOtherHover {
           width:100px;
           height:100px;
           background-color:green;
}

Here is my javascript.js: 这是我的javascript.js:

function hoverImplicitly {
           $(".hidden").toggleClass('visible');
}

When I hover the mouse on the div.triggerOtherHover , the other two divs with .hidden class become red cause of the opacity, but when I remove the mouse from the div.triggeOtherHover, the divs are still red. 当我将鼠标悬停在div.triggerOtherHover上时,具有.hidden类的其他两个div变成不透明的红色原因,但是当我从div.triggeOtherHover中删除鼠标时,div仍然是红色的。 I would like them to become transparent (opacity: 0) again. 我希望它们再次变得透明(不透明度:0)。 Is there a way to make them behave like the CSS :hover pseudo class? 有没有办法让它们表现得像CSS:hover伪类?

You need to add a mouseout handler to the div as well, so that the class gets removed when focused out of it. 您还需要向div中添加一个mouseout处理程序,以便在将焦点移出该类时将其删除。

   <div class="triggerOtherHover" onmouseover="hoverImplicitly();" onmouseout="hoverImplicitly();">

Or instead of adding inline handler bind the event to the element using hover pseudoevent. 或者,而不是添加内联处理程序,而是使用hover伪事件将事件绑定到元素。

 $(function(){
    $('.triggerOtherHover').hover(hoverImplicitly); //hover represent mouseenter, mouseleave
});

Also note that you are missing parens () in the function declaration function hoverImplicitly() 还要注意的是你缺少括号()函数声明function hoverImplicitly()

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

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