悬停并单击

时间:2018-04-13 15:47:29

标签: javascript jquery html css

我有4个这样的div-s,当我将鼠标悬停在其中一个链接上时,所有元素都获得相同的代码。请帮助我的代码。

        <div class="Person-team">
            <div class="profile-pic-d">
                <a class="profile-pic-a">
                    <img class="profile-picture" alt="profile picture" src="img/profile.jpg">
                </a>
            </div>
        </div>

我有这样的CSS

.profile-picture {
height: 200px;
width: 200px;
border-radius: 100px;
}

.profile-picture.hover {
padding: 10px;
border: 6px solid #ffdd00;
}

.profile-picture.click {
padding: 10px;
border: 6px solid #ffdd00;
}

和js在这里:

$(".profile-pic-a").hover(
    function() {
        $(".profile-picture").toggleClass('hover');
    }
);

$(".profile-pic-a").click(
    function() {
        $(".profile-picture").toggleClass('click');
    }
);

2 个答案:

答案 0 :(得分:1)

尝试限制匹配的集合:

$(".profile-pic-a").hover(
    function() {
        $(this).find(".profile-picture").toggleClass('hover');
    }
);

$(".profile-pic-a").click(
    function() {
        $(this).find(".profile-picture").toggleClass('click');
    }
);

代码$(this).find(".profile-picture").toggleClass('click');仅在作为悬停/点击元素的子代的.profile-picture上切换类。

$(".profile-pic-a").hover(
  function() {
    $(this).find(".profile-picture").toggleClass('hover');
  }
);

$(".profile-pic-a").click(
  function() {
    $(this).find(".profile-picture").toggleClass('click');
  }
);
.profile-picture {
  height: 200px;
  width: 200px;
  border-radius: 100px;
}

.profile-picture.hover {
  padding: 10px;
  border: 6px solid #ffdd00;
}

.profile-picture.click {
  padding: 10px;
  border: 6px solid #ffdd00;
  background: red; /* added for demo */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Person-team">
  <div class="profile-pic-d">
    <a class="profile-pic-a">
      <img class="profile-picture" alt="profile picture" src="img/profile.jpg">
    </a>
  </div>
</div>
<br><br><br><br>
<div class="Person-team">
  <div class="profile-pic-d">
    <a class="profile-pic-a">
      <img class="profile-picture" alt="profile picture" src="img/profile.jpg">
    </a>
  </div>
</div>


值得一提的是,正如@AndrewBone在评论中所指出的那样,您可以使用:hover CSS伪类代替该代码:

// the hover behavior can be replaced by `:hover` CSS pseudo-class

$(".profile-pic-a").click(
  function() {
    $(this).find(".profile-picture").toggleClass('click');
  }
);
.profile-picture {
  height: 200px;
  width: 200px;
  border-radius: 100px;
}

.profile-picture:hover { /* using :hover now */
  padding: 10px;
  border: 6px solid #ffdd00;
}

.profile-picture.click {
  padding: 10px;
  border: 6px solid #ffdd00;
  background: red; /* added for demo */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Person-team">
  <div class="profile-pic-d">
    <a class="profile-pic-a">
      <img class="profile-picture" alt="profile picture" src="img/profile.jpg">
    </a>
  </div>
</div>
<br><br><br><br>
<div class="Person-team">
  <div class="profile-pic-d">
    <a class="profile-pic-a">
      <img class="profile-picture" alt="profile picture" src="img/profile.jpg">
    </a>
  </div>
</div>

答案 1 :(得分:0)

如果您希望对每个使用ID产生不同的影响而不是类,并且ID是唯一的,因此属性将仅应用于单个。