将类添加到悬停时共享类的另一个元素

耶尔姆

我有两个容器,一个是盒子的网格,每个盒子内有文本,另一个是链接列表。

在每个容器中,一个盒子和一个物品将共享一个类。

我想将鼠标悬停在链接之一上,并将类添加到与该链接共享同一类的框的子级中。

我可以通过使用

$(function(){
$(".item-list").children(".class-one").mouseover(function() {
  $("#grid").children(".class-one").children("h1").addClass("red").siblings("p").addClass("purple");
    });
    $(".item-list").children(".class-one").mouseleave(function() {
    $("#grid").children(".class-one").children("h1").removeClass("red").siblings("p").removeClass("purple");
    });
    $(".item-list").children(".class-two").mouseover(function() {
    $("#grid").children(".class-two").children("h1").addClass("red").siblings("p").addClass("purple");
    });
    $(".item-list").children(".class-two").mouseleave(function() {
    $("#grid").children(".class-two").children("h1").removeClass("red").siblings("p").removeClass("purple");
    });
});
.container {
  display: flex;
  justify-content: center;
  align-items:flex-start;
}

#grid .box {
  padding: 2rem;
}

#grid .box h1, p {
  font-family: sans-serif;
  margin: 1rem;
  color: blue;
}

.item-list {
  display: flex;
  flex-direction: column;
}

.item-list a {
  display: inline-block;
  margin: 1rem 0;
  color: goldenrod;
  font-weight: bold;
  text-decoration: none;
  font-family: sans-serif;
}


.item-list a:hover {
  text-decoration: underline;
}

.red {
  color: red !important;
  text-decoration: underline;
}

.purple {
  color: purple !important;
  text-decoration: underline;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div id="grid">
    <div class="box class-one">
      <h1>Box One</h1>
      <p>box one right here</p>
    </div>
    <div class="box class-two">
      <h1>Box Two</h1>
      <p>box two right here</p>
    </div>
  </div>
  <div class="item-list">
    <a class="class-one">Item One</a>
    <a class="class-two">Item Two</a>
  </div>
</div>

但这似乎很快就会失去控制,因为我计划有很多相同的类,而且我不想一遍又一遍地重复相同的代码。

任何帮助是极大的赞赏。

瑞安·威尔逊(Ryan Wilson)

您可以为此使用event delegationJQuery .on

 $(function(){
        //This fires the event whenever an anchor element of element with 
        //className item-list has mouseover
        $(".item-list").on('mouseover', 'a', function(){
            //Get the class of the anchor which corresponds to the box
            const anchorClasses = $(this).attr("class").split(/\s+/); //Get the classes of the anchor
            const anchorClassIWant = anchorClasses.filter(function(c) { return c.indexOf("class-") > -1 ;});
            //use the class of the anchor in our selector to get the proper grid elements
            $("#grid").children("." + anchorClassIWant).children("h1").addClass("red").siblings("p").addClass("purple");
        });
        
         //This fires the event whenever an anchor element of element with 
        //className item-list has mouseleave
        $(".item-list").on('mouseleave', 'a', function(){
             //Get the class of the anchor which corresponds to the box
            const anchorClasses = $(this).attr("class").split(/\s+/); //Get the classes of the anchor
            const anchorClassIWant = anchorClasses.filter(function(c) { return c.indexOf("class-") > -1 ;});
            //use the class of the anchor in our selector to get the proper grid elements
            $("#grid").children("." + anchorClassIWant ).children("h1").removeClass("red").siblings("p").removeClass("purple");
        });

});
.container {
  display: flex;
  justify-content: center;
  align-items:flex-start;
}

#grid .box {
  padding: 2rem;
}

#grid .box h1, p {
  font-family: sans-serif;
  margin: 1rem;
  color: blue;
}

.item-list {
  display: flex;
  flex-direction: column;
}

.item-list a {
  display: inline-block;
  margin: 1rem 0;
  color: goldenrod;
  font-weight: bold;
  text-decoration: none;
  font-family: sans-serif;
}


.item-list a:hover {
  text-decoration: underline;
}

.red {
  color: red !important;
  text-decoration: underline;
}

.purple {
  color: purple !important;
  text-decoration: underline;
}

   
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div id="grid">
    <div class="box class-one">
      <h1>Box One</h1>
      <p>box one right here</p>
    </div>
    <div class="box class-two">
      <h1>Box Two</h1>
      <p>box two right here</p>
    </div>
  </div>
  <div class="item-list">
    <a class="class-one">Item One</a>
    <a class="class-two">Item Two</a>
  </div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

仅当添加了另一个类时,才将类添加到元素中

来自分类Dev

将另一个类中的元素添加到数组列表中

来自分类Dev

如果元素具有类,则将类添加到另一个元素

来自分类Dev

将另一个类的项目添加到另一个类的属性。目标C

来自分类Dev

将另一个类的项目添加到另一个类的属性。物镜

来自分类Dev

当一个元素悬停并影响CSS中的另一个元素/类时,如何添加悬停效果?

来自分类Dev

如何删除元素列表中的类并添加到另一个元素Jquery?

来自分类Dev

Angular指令未将CSS类添加到另一个元素

来自分类Dev

为什么可以将一个类的对象添加到另一个类的LinkedList中?

来自分类Dev

如何将一个类中的对象添加到另一个类中的List。

来自分类Dev

将JPanel从一个类添加到另一个类中的JPanel

来自分类Dev

滚动到另一个元素时尝试将class添加到元素

来自分类Dev

如何将类添加到另一个 div,当一个 div 具有切换类时?

来自分类Dev

将数据添加到另一个类的TableView中

来自分类Dev

将另一个类中的对象添加到JPanel

来自分类Dev

Java将值添加到另一个类的数组

来自分类Dev

是否可以使用jquery将类添加到另一个页面?

来自分类Dev

将数据添加到另一个类的arrayList中

来自分类Dev

将方法添加到另一个项目的类中

来自分类Dev

将另一个类添加到 Django 中的序列化程序

来自分类Dev

将另一个类行添加到对象数组

来自分类Dev

将列表添加到另一个列表类

来自分类Dev

当一个类添加到另一个类中时,在容器类上添加 CSS,反之亦然

来自分类Dev

通过tkinter 0中另一个类的方法将标签添加到框架类

来自分类Dev

我可以将类作为扩展添加到 Kotlin 中的另一个类吗?

来自分类Dev

AngularJS指令,在单击时添加类,但是如果单击,则将其删除并添加到另一个元素中

来自分类Dev

AngularJS指令,可在单击时添加类,但如果单击,则将其删除并将其添加到另一个元素中

来自分类Dev

如何将一个类添加到div,使其等待一会儿,然后将另一个类添加到另一个div?

来自分类Dev

将类(悬停时)添加到目标池中,但不添加到悬停的元素上

Related 相关文章

  1. 1

    仅当添加了另一个类时,才将类添加到元素中

  2. 2

    将另一个类中的元素添加到数组列表中

  3. 3

    如果元素具有类,则将类添加到另一个元素

  4. 4

    将另一个类的项目添加到另一个类的属性。目标C

  5. 5

    将另一个类的项目添加到另一个类的属性。物镜

  6. 6

    当一个元素悬停并影响CSS中的另一个元素/类时,如何添加悬停效果?

  7. 7

    如何删除元素列表中的类并添加到另一个元素Jquery?

  8. 8

    Angular指令未将CSS类添加到另一个元素

  9. 9

    为什么可以将一个类的对象添加到另一个类的LinkedList中?

  10. 10

    如何将一个类中的对象添加到另一个类中的List。

  11. 11

    将JPanel从一个类添加到另一个类中的JPanel

  12. 12

    滚动到另一个元素时尝试将class添加到元素

  13. 13

    如何将类添加到另一个 div,当一个 div 具有切换类时?

  14. 14

    将数据添加到另一个类的TableView中

  15. 15

    将另一个类中的对象添加到JPanel

  16. 16

    Java将值添加到另一个类的数组

  17. 17

    是否可以使用jquery将类添加到另一个页面?

  18. 18

    将数据添加到另一个类的arrayList中

  19. 19

    将方法添加到另一个项目的类中

  20. 20

    将另一个类添加到 Django 中的序列化程序

  21. 21

    将另一个类行添加到对象数组

  22. 22

    将列表添加到另一个列表类

  23. 23

    当一个类添加到另一个类中时,在容器类上添加 CSS,反之亦然

  24. 24

    通过tkinter 0中另一个类的方法将标签添加到框架类

  25. 25

    我可以将类作为扩展添加到 Kotlin 中的另一个类吗?

  26. 26

    AngularJS指令,在单击时添加类,但是如果单击,则将其删除并添加到另一个元素中

  27. 27

    AngularJS指令,可在单击时添加类,但如果单击,则将其删除并将其添加到另一个元素中

  28. 28

    如何将一个类添加到div,使其等待一会儿,然后将另一个类添加到另一个div?

  29. 29

    将类(悬停时)添加到目标池中,但不添加到悬停的元素上

热门标签

归档