是否可以更改字段集的背景颜色输入:focus? [英] Is it possible to change a fieldset's background-color on input:focus?

查看:131
本文介绍了是否可以更改字段集的背景颜色输入:focus?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当光标位于该字段集的任何文本字段内时,是否可以更改表单字段集的背景颜色?

Is it possible to have the background-color of a form's fieldset change when the cursor is inside any of that fieldset's text fields?

我假设这可能有效,但不会:

I assumed this might work, but it doesn't:

fieldset {background: #ffe;}
input[type=text]:focus+fieldset {background: #ff0;}


推荐答案

我恐怕这是不可能的CSS,因为CSS没有一个选择器,将选择基于元素的孩子。在您的尝试中的选择器 input [type = text]:focus + fieldset 匹配 fieldset

I’m afraid it’s not possible with CSS, since CSS hasn’t got a selector that would select on the basis of an element’s children. The selector input[type=text]:focus+fieldset in your attempt matches a fieldset element that immediately follows a focused text input box—something quite different from what you want.

然而,使用JavaScript可以很容易做到这一点。你只需要在字段集中的字段上使用onfocus和onblur事件处理程序,并且这些处理程序对于所有这些处理程序可以是相同的函数;它们只会更改字段集元素的 style.background 属性,

It is however possible and fairly easy do deal with this using JavaScript. You would just need onfocus and onblur event handlers on the fields inside the fieldset, and these handlers could be the same functions for all of them; they would just change the style.background property of the fieldset element,

这篇关于是否可以更改字段集的背景颜色输入:focus?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆