影响 :focus'd 元素的父元素(首选纯 CSS+HTML)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2212583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:32:16  来源:igfitidea点击:

Affecting parent element of :focus'd element (pure CSS+HTML preferred)

cssfocuscss-selectors

提问by Shane Daniel

How can I change the background of a parent <div>when an <input>or <a>is :focus'd (or any other dynamic pseudo-class?

我怎样才能改变父母的背景<div>的时候<input>或者<a>是:focus'd(或任何其他动态伪类?

Eg

例如

<div id="formspace">
<form>
<label for="question">
How do I select #formspace for the case when a child is active?</label>
<input type="text" id="question" name="answer"/></form></div>

采纳答案by Rowno

Unfortunately css doesn't support parent selectors. :(
So the only way to do it is to use javascript like the jQuery parent method.

不幸的是 css 不支持父选择器。:(
所以唯一的方法是像jQuery父方法一样使用javascript 。

Update:CSS Selectors Level 4 will support parent selectors! http://www.w3.org/TR/selectors4/#subject

更新:CSS Selectors Level 4 将支持父选择器!http://www.w3.org/TR/selectors4/#subject

回答by anthares

The only way I can think of is with JavaSCript:

我能想到的唯一方法是使用 JavaSCRipt:

function focusLink(isOnFocus)
{
    if (isOnFocus)
      document.getElementById('formspace').className = "<make a focus class name here>";
    else
      document.getElementById('formspace').className = "<default class name here>";
}

...

...

<input onFocus="focusLink(true)" onBlur="focusLink(false)">

Hope that helps!

希望有帮助!