是否可以更改输入上字段集的背景颜色:焦点?
当光标位于该字段集的任何文本字段内时,是否可以更改表单字段集的背景颜色?
我以为这可能有效,但事实并非如此:
fieldset {background: #ffe;}
input[type=text]:focus+fieldset {background: #ff0;}
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;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法根据其子
input
之一的焦点状态来设置fieldset
的样式。但是,您可以通过添加一个空的
div
作为fieldset
的最后一个子元素并设置其样式来模拟效果。然后可以使用焦点输入上的通用同级选择器来更改此div
的样式:You can't style a
fieldset
based on the focus state of one of its childreninput
s.However, you can simulate the effect by adding an empty
div
as the last child of thefieldset
, and styling it. Thisdiv
's styles can then be changed using the general sibling selector on the focusedinput
:现在可以使用
:focus-within
This is now possible with
:focus-within
恐怕这对于 CSS 来说是不可能的,因为 CSS 没有可以根据元素的子元素进行选择的选择器。您尝试中的选择器
input[type=text]:focus+fieldset
与紧随聚焦文本输入框的fieldset
元素相匹配 - 某些内容与你想要的完全不同。然而,使用 JavaScript 可以并且相当容易地处理这个问题。您只需要在字段集中的字段上使用 onfocus 和 onblur 事件处理程序,并且这些处理程序对于所有这些处理程序来说可以是相同的函数;他们只需更改
fieldset
元素的style.background
属性,I’m afraid it’s not possible with CSS, since CSS doesn't have a selector that would select on the basis of an element’s children. The selector
input[type=text]:focus+fieldset
in your attempt matches afieldset
element that immediately follows a focused text input box—something quite different from what you want.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 thefieldset
element,如果您出于可访问性原因不使用字段集,则只需执行以下操作:
http:// /www.pmob.co.uk/temp/categorybox.htm
如果您需要它用于边框和可访问性,请考虑将字段集包装在 div 中,然后对包含 div 而不是字段集的样式进行样式化。
希望这有帮助!
马特
If you are not using the fieldset for accessibility reasons, then just do something like this:
http://www.pmob.co.uk/temp/categorybox.htm
If you need it for both borders AND accessibility, consider wrapping the fieldset in a div and then styling that containing div instead of the fieldset.
Hope this helps!
Matt
如果您使用 Jquery,并且没有嵌套字段集,则可以执行简单的绑定,将其自身附加到字段集中的所有页面控件,并且每当您聚焦/取消聚焦任何这些控件时,都会添加/删除一个类来自包含控件的字段集。
这是一个示例:
<代码>
$('输入、标签、选择、选项、按钮', '字段集').each(function (index, item) {
$(this).focus(function () { $(this).closest('fieldset').addClass('fieldsetFocus'); });
$(this).blur(function () { $(this).closest('fieldset').removeClass('fieldsetFocus'); });
});
If you are using Jquery, and you are not nesting your fieldsets, you can do a simple bind which attaches itself to all your page controls within a fieldsets, and whenever you focus/unfocus on any of these controls, a class is added/removed from the fieldset containing the control.
Here's a sample:
$('input, label, select, option, button', 'fieldset').each(function (index, item) {
$(this).focus(function () { $(this).closest('fieldset').addClass('fieldsetFocus'); });
$(this).blur(function () { $(this).closest('fieldset').removeClass('fieldsetFocus'); });
});