简体   繁体   English

CSS:悬停在元素的子元素上应该影响不同的元素

[英]CSS: Hovering over child of element should affect different element

I want users to be able to hover over a button within a different div , which then changed the background-color of the div below.我希望用户能够将hover在不同divbutton ,然后更改下面divbackground-color

My current code is as followed:我目前的代码如下:

 .delSeperator button:hover+.linkField { background-color: rgba(255, 0, 0, 0.2); } .seperator { color: #000000; background-color: #e4e8eb; min-height: 0.5rem; margin: 1rem 0rem 1rem 0rem; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <div id="delSeperator_0" class="row delSeperator"> <div class="col-xs-10 mb-0"> <div class='seperator vertical-center'></div> </div> <div class="col-xs-1 mb-0"><button type="button" class="btn btn-danger" onclick="delField('linkField', this.closest('.row')); delField('delSeperator', this.closest('.row'));"><i class="fa fa-minus" aria-hidden="true"></i></button></div> </div> <div class="row linkField" id="linkField_0"> <div class="col-xs-6 mb-0"> <div class="form-group"><label for="txt_link[0][&quot;bezeichnung&quot;]" class="">Bezeichnung</label><input id="txt_link[0][&quot;bezeichnung&quot;]" type="text" maxlength="200" name="link[0][&quot;bezeichnung&quot;]" class="form-control"></div> </div> <div class="col-xs-6 mb-0"> <div class="form-group"><label for="txt_link[0][&quot;url&quot;]" class="">URL</label><input id="txt_link[0][&quot;url&quot;]" type="url" maxlength="500" name="link[0][&quot;url&quot;]" class="form-control"></div> </div> </div>

Changing the Hover-CSS to将 Hover-CSS 更改为

.delSeperator:hover + .linkField {
    background-color: rgba(255,0,0,0.2);
}

works so that when you hover over the whole .delSeperator , the .linkField gets the background property.工作,以便当您将鼠标悬停在整个.delSeperator.linkField获得背景属性。

When I change the code to当我将代码更改为

.delSeperator button:hover {
    background-color: rgba(255,0,0,0.2);
}

it works so that when you hover over the button , the button gets the background property.它的工作原理是,当您将hoverbuttonbutton会获得background属性。

But I can't seem to combine these two.但我似乎无法将这两者结合起来。 Is there any way to achieve what I need?有什么方法可以实现我所需要的吗?

You can workaround this issue by playing with the pointer-events property您可以通过使用pointer-events属性来解决此问题

i got this to work with the following CSS:我让它与以下 CSS 一起使用:

.delSeperator:hover + .linkField {
background-color: rgba(255,0,0,0.2);
}

.delSeperator {
  pointer-events: none
}

.delSeperator button {
  pointer-events: initial
}

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

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