简体   繁体   English

jQuery:在主元素上悬停时如何悬停子元素的子元素

[英]jQuery: How to hover a child of child element when hovering on the main element

I have this code: 我有以下代码:

$('.icon-click').mouseenter(function () {
  console.log('banana');
   $(this).hover(function(){
     $(this).children("img");
   });
});
<div class="circles-cont">
  <div class="col-sm-4 text-center icon-click">
    <p class="circle">
      <img class="search" src="http://www.clker.com/cliparts/w/N/I/K/O/9/smaller-red-button-th.png">
    </p>
    <p class="thetext">
      <span>Search</span>
      <br/>
      <span>afasfasfa</span>
    </p>
  </div>

  <div class="col-sm-4 text-center icon-click">
    <p class="circle ">
      <img class="conv" src="http://www.clker.com/cliparts/w/N/I/K/O/9/smaller-red-button-th.png">
    </p>
    <p class="thetext">
      <span>asfsfas</span>
      <br/>
      <span>asfasfasf</span>
    </p>
  </div>

  <div class="col-sm-4 text-center icon-click">
    <p class="circle ">
      <img class="local" src="http://www.clker.com/cliparts/w/N/I/K/O/9/smaller-red-button-th.png">
    </p>
    <p class="thetext">
      <span>asfasasf</span>
    </p>
  </div>

I am trying to get the result of the img:hover for each element sepretly only when hovering on it's main .icon-click element. 我试图仅将鼠标悬停在其主要.icon-click元素上时分别获取img:hover的结果。

https://jsfiddle.net/0rwe5z23/2/ https://jsfiddle.net/0rwe5z23/2/

You can't use hover to take effect of the :hover selecter on css classes. 您不能使用hover使css类上的:hover生效。 But you can create a class with your changes and redirect this by jQuery's $.hover function to the elements you want. 但是您可以使用您的更改创建一个类,并通过jQuery的$.hover函数将此重定向到所需的元素。

$(".icon-click").hover(function() {
    $(this).find("img").addClass("hovered");
}, function() {
    $(this).find("img").removeClass("hovered");
});

Working example. 工作示例。

But you can even do the whole thing without js. 但是您甚至可以在没有js的情况下完成整个工作。 Just use the :hover on the parent. 只需在父级上使用:hover Like this: 像这样:

.icon-click:hover img {
    background-color: black;
}

Working example. 工作示例。

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

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