javascript - 为什么 jquery 将鼠标悬停在一个元素上,突出显示所有元素

标签 javascript jquery html css

当我将鼠标悬停在一个元素上时,所有元素都会突出显示 这是我的 html 和 jquery 代码

    $('.hover-text').hide();
       $('.movie-content').hover(
         function () {
            $('.movies_post_text').hide();
            $('.hover-text').show();
         }, 
         function () {
            $('.movies_post_text').show();
            $('.hover-text').hide();
         }
      );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="movie-content movie-big" style="background-image: url('Images/incidentbyabank-900x498.jpg');">
<a href="http://www.cricbuzz.com">

  <div class="movies_post_text">
    <h3>INCIDENT BY A BANK</h3>
    <p>Sweden/12 MIN</p>
    <p>Award Winning, Drama, Thriller</p>
    <p>DIR.Ruben Ostlund</p>
  </div>

  <div class="hover-text">
    <div class="row movie-info">
      <div class="col-md-4">
        <div class="reactions pull-left">
          <div class="view">429<i class="fa fa-eye"></i></div>
          <div class="like">252<i class="fa fa-thumbs-up"></i></div>
        </div>
      </div>
      <div class="col-md-8">
        <h3 class="grid-title">INCIDENT BY A BANK</h3>
        <div class="movie-desc">
          <p>Shot using a single camera, 90 people meticulously recreate a failed bank robbery that took place in Stockholm in June 2006. A superb single shot.The short went on to win the Golden Bear at </p>
        </div>
      </div>
    </div>
  </div>

</a>
</div>

请建议我使用 jquery 或任何 html 类来解决这个问题。帮助我摆脱它我知道如果我使用 this 它会得到解决但是如何使用它来开始工作

最佳答案

问题在于,在您的事件回调中,您并未将选择器限制为仅在悬停的父级内运行。

$('.hover-text').hide();
   $('.movie-content').hover(
     function () {
        $('.movies_post_text').hide(); //<-- all matching elements, not just the
                                       //    one inside the hovered div
        $('.hover-text').show();       //<-- same here
     }, 
     function () {
        $('.movies_post_text').show(); //<-- " "
        $('.hover-text').hide();       //<-- " "
     }
  );

应该是

$('.hover-text').hide();
   $('.movie-content').hover(
     function () {
        $(this).find('.movies_post_text').hide();
        $(this).find('.hover-text').show();
     }, 
     function () {
        $(this).find('.movies_post_text').show();
        $(this).find('.hover-text').hide();
     }
  );

关于javascript - 为什么 jquery 将鼠标悬停在一个元素上,突出显示所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54387688/

相关文章:

php - Amazon S3 和 jqueryfileupload 插件的图像上传性能问题

html - <br> 在 Bootstrap 中不换行

javascript - 如何在网页中的 Flash 小程序上模拟假的 MouseOver?

Javascript Div 翻转

javascript - 如果我等待 2 个函数,我可以保证返回对象将具有两个值

javascript - 具有多个参数的数据库搜索

javascript - 没有错误时什么也没有显示。代码运行良好

javascript - 在 Javascript 中将 10 个循环合并为一个

jquery - 将阴影添加到文本框 onFocus

javascript - 在 jQuery UI 中使用效果时,早期 DOM 元素不会隐藏