简体   繁体   English

将鼠标悬停在某个元素上可以对不相邻或同级的另一个元素生效

[英]Hover over an element to put effect on another element that is not adjacent or a sibling

I know that it is possible to achieve this using jquery but I want an approach it without jquery since I am working with an angular app. 我知道可以使用jquery来实现此目的,但是我想要一种无需jquery的方法,因为我正在使用角度应用程序。 What I want to do is when I hover over an img element, I want to put text-decoration: underline in div with class title . 我想要做的是,当我将鼠标悬停在img元素上时,我想将text-decoration: underline放在class title div中。 Keep in mind however that the elements are neither siblings nor adjacent to each other. 但是请记住,这些元素既不是兄弟姐妹,也不是彼此相邻。 Is it possible to achieve this in any way using CSS or maybe something of Angular? 是否有可能使用CSS或Angular的任何方式实现这一目标?

<div class='big class'>
     <div class='title'>
          <p> This is a text that I want to be underlined when hovering over the img element </p>
     </div>
     <div>..........</div>
     <div>..........</div>
     <div>..........</div>
     <img class='image' [src]="some image source"/>
</div>

Indeed, you don't need jquery, Angular has it all :) 确实,您不需要jquery,Angular拥有了所有的功能:)

You would need to add mouseneter and mouseleave events to the img tag with a property to set hovered state and change the class attribute on the title tag. 您需要使用属性来将mouseneter和mouseleave事件添加到img标签,以设置悬停状态并更改title标签上的class属性。 The rest is in CSS 其余的在CSS中

HTML: HTML:

<div class='big class'>
     <div class='title' [class.foo]="hovered">
          <p> This is a text that I want to be underlined when hovering over the img element </p>
     </div>
     <div>..........</div>
     <div>..........</div>
     <div>..........</div>
     <img class='image' (mouseenter)="hovered=true" (mouseleave)="hovered=false" 
 [src]="some image source"/>
</div>

CSS CSS

.foo{
  text-decoration: underline;
  text-decoration-color: red
}

Demo 演示

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

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