简体   繁体   English

使用CSS,当我将鼠标悬停在父元素上时,可以更改子元素的属性吗?

[英]With CSS, can I change the property of a child element when I hover on a parent element?

I have a div and a span inside of it (with some text and a border). 我里面有一个div和一个span(带有一些文本和边框)。

<div id='div1'>
    <span id='span1' style='border-right: 1px black solid'>Some text</span>
</div>

What I want to do is: when I hover my mouse over that div, I would like to remove the border from the span. 我想做的是:当我将鼠标悬停在那个div上时,我想从范围中删除边框。 And when I move out of the div, I want the border to come back as it was. 当我离开div时,我希望边界恢复原状。

Can I do this with CSS or should I resort to JS/jQuery? 我可以使用CSS还是使用JS / jQuery?

Thanks a lot in advance. 非常感谢提前。

You don't need JavaScript, you can use css: 您不需要JavaScript,可以使用CSS:

#div1:hover #span1 {
    border-width: 0;
}

Quite simply :-) 很简单:-)

You can't use :hover for inline styles, so you'll have to move them to style tags or style sheets. 不能将:hover用于内联样式,因此必须将它们移至样式标签或样式表。

#div1:hover span{
    border:0;
}
#div1 span{
    border-right: 1px black solid;
}

Yes it's rather easy to target the selector after a :hover state selector. 是的,在:hover状态选择器之后将选择器作为目标很容易。 jsfiddle created here: http://jsfiddle.net/wigster/a4V8Q/ jsfiddle在这里创建: http : //jsfiddle.net/wigster/a4V8Q/

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

相关问题 css悬停元素来显示孩子,当我将鼠标悬停在孩子身上时,如何将悬停css保留在父母身上? - css hover element to show child, how can I keep the hover css on the parent when I mouseover the child? 将鼠标悬停在子元素上时更改父元素的CSS属性 - Change css properties of parent element when hover on child element 将鼠标悬停在父元素上时,如何更改子元素? - How can I make the child element change when hover is applied on the parent? 当将鼠标悬停在其子元素上时,如何保持父元素的背景颜色不变? - how can I keep parent element's background-color unchanged when hover on its child element? 当我 hover cursor 在子元素上时所有父元素移动 - all parent elements moving when i hover the cursor on the child element 将鼠标悬停在其子div元素上时可以隐藏父div吗? - Can I hide a parent div when I hover on it's child div element? 将鼠标悬停在子元素上时更改父元素的颜色 - change color of parent element when hover over child element 如果它不是子元素,纯CSS可以在悬停时更改其他元素吗? - Can pure CSS change other element on hover if its not child element? CSS / JS解决方案:将鼠标悬停在子元素上时,更改父div - CSS/JS Solution: On hover of child element, change parent div CSS /在悬停时,我可以更改同一元素的:: after吗? - CSS / On hover, can I change the same element's ::after?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM