【问题标题】:JavaScript - Apply function to a specific div in a collection of divs with the same class nameJavaScript - 将函数应用于具有相同类名的 div 集合中的特定 div
【发布时间】:2017-06-17 19:34:25
【问题描述】:

当我将鼠标悬停在.winner-container 上时,JS 函数告诉.headline 移出.winner-container,它告诉.bottom 移入.winner-container。当我取消悬停时,会发生相反的情况。

问题是,我将拥有数百个这样的容器,它们都带有.winner-container 类。所以我意识到,当我将鼠标悬停在其中一个容器上时,该功能会同时应用于数百个不同的容器。我只希望将该功能应用于我悬停的特定容器。我可以通过给每个容器一个 id 来做到这一点,然后为每个 id 编写新的 JS 代码,但考虑到将有数百个这样的 div,这将需要大量工作。有没有更优雅的解决方案?

https://jsfiddle.net/6sm6ajht/

HTML

<div class="winner-container">
  <div class="top">
    <h4 class="headline">SME Example 1</h4>
  </div>
  <div class="bottom">
    <div class="winner-words">
      <h6>SME Examle 1 is a technology company.</h6>
      <h6><a>Learn more...</a></h6>
    </div>
  </div>
</div>

<div class="winner-container">
  <div class="top">
    <h4 class="headline">SME Example 2</h4>
  </div>
  <div class="bottom">
    <div class="winner-words">
      <h6>SME Examle 2 is an e-commerce company.</h6>
      <h6><a>Learn more...</a></h6>
    </div>
  </div>
</div>

CSS

.winner-container {
  position: relative;
  box-shadow: 0px 2.5px 1px 0px rgba(0, 0, 0, 0.25);
  border: 1px solid #074E68;
}

.winner-container,
.top,
.bottom {
  width: 10em;
  height: 12em;
  overflow: hidden;
}

.bottom {
  position: absolute;
  height: 12em;
  width: 100%;
  top: 12em;
  transition: 0.5s ease-in-out;
}

.top .headline {
  position: absolute;
  top: 2.5em;
  width: 100%;
  background: rgba(255, 255, 255, 0.8);
  box-shadow: 0px 1px 1px 2px rgba(0, 0, 0, 0.1);
  transition: 0.5s ease-in-out;
}

.top-up .headline {
  top: -2.5em;
}

.bottom-up.bottom {
  top: 0em;
  background-color: rgba(0, 0, 0, 0.65);
}

JavaScript

$(".winner-container").on("mouseenter", function() {
  $(".top").addClass('top-up');
  $(".bottom").addClass('bottom-up');
});

$(".winner-container").on("mouseleave", function() {
  $(".top").removeClass('top-up');
  $(".bottom").removeClass('bottom-up');
});

【问题讨论】:

  • 你应该为你需要单独管理的标签添加一个id

标签: javascript jquery html css hover


【解决方案1】:

这是$(this) 选择器的绝佳机会。因为有许多相同的元素,但您只希望每个事件处理程序引用该特定元素,您可以使用 $(this) 并使用像 .children 这样的相对选择器来定位与 this 元素相关的其他元素。

JSfiddle

$(".winner-container").on("mouseenter", function() {
  $(this).children(".top").addClass('top-up');
  $(this).children(".bottom").addClass('bottom-up');
});

$(".winner-container").on("mouseleave", function() {
  $(this).children(".top").removeClass('top-up');
  $(this).children(".bottom").removeClass('bottom-up');
});

【讨论】:

    【解决方案2】:

    将 .top 和 .bottom 的选择器更改为 $(this).find('.top')$(this).find('.bottom')

    事件处理程序上下文中的 this 是发生事件的元素。

    【讨论】:

      【解决方案3】:

      您可以尝试使用 $(this) 选择器来指定您只希望当前元素成为指令的范围:

      $(".winner-container").on("mouseenter", function() {
        $(this).children(".top").addClass('top-up');
        $(this).children(".bottom").addClass('bottom-up');
      });
      
      $(".winner-container").on("mouseleave", function() {
        $(this).children(".top").removeClass('top-up');
        $(this).children(".bottom").removeClass('bottom-up');
      });
      

      【讨论】:

      • 投了反对票,因为您的答案只是 dogui 的副本。
      • 我没有看这个人的答案,否则我根本没有兴趣回答。我们同时发布了我们的答案。这将教会我如何帮助他人。
      【解决方案4】:

      $(".winner-container") 将针对所有具有 winner-container 类的元素并为每个元素添加一个事件,这就是为什么你有两个元素但你只需要定义一次。

      $('.top') 相同,这将针对所有名为top 的类,您需要在事件中使用$(this).find('.top') 仅针对当前元素并使用find() 在该元素内搜索,找到top 和@ 987654326@添加或删除类随心所欲。

      $(".winner-container").on("mouseenter", function() {
        $(this).find('.top').addClass('top-up');
        $(this).find('.bottom').addClass('bottom-up');
      });
      
      $(".winner-container").on("mouseleave", function() {
        $(this).find('.top').removeClass('top-up');
        $(this).find('.bottom').removeClass('bottom-up');
      });
      .winner-container {
        position: relative;
        box-shadow: 0px 2.5px 1px 0px rgba(0, 0, 0, 0.25);
        border: 1px solid #074E68;
      }
      
      .winner-container,
      .top,
      .bottom {
        width: 10em;
        height: 12em;
        overflow: hidden;
      }
      
      .bottom {
        position: absolute;
        height: 12em;
        width: 100%;
        top: 12em;
        transition: 0.5s ease-in-out;
      }
      
      .top .headline {
        position: absolute;
        top: 2.5em;
        width: 100%;
        background: rgba(255, 255, 255, 0.8);
        box-shadow: 0px 1px 1px 2px rgba(0, 0, 0, 0.1);
        transition: 0.5s ease-in-out;
      }
      
      .top-up .headline {
        top: -2.5em;
      }
      
      .bottom-up.bottom {
        top: 0em;
        background-color: rgba(0, 0, 0, 0.65);
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="winner-container">
        <div class="top">
          <h4 class="headline">SME Example 1</h4>
        </div>
        <div class="bottom">
          <div class="winner-words">
            <h6>SME Examle 1 is a technology company.</h6>
            <h6><a>Learn more...</a></h6>
          </div>
        </div>
      </div>
      
      <div class="winner-container">
        <div class="top">
          <h4 class="headline">SME Example 2</h4>
        </div>
        <div class="bottom">
          <div class="winner-words">
            <h6>SME Examle 2 is an e-commerce company.</h6>
            <h6><a>Learn more...</a></h6>
          </div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2022-07-27
        • 2020-03-20
        • 2016-07-27
        • 2014-02-11
        • 1970-01-01
        • 2017-12-10
        相关资源
        最近更新 更多