2011-12-29 64 views

回答

1

恐怕是不可能的CSS,CSS以來一直沒有得到一個選擇,將一個元素的兒童的基礎上進行選擇。您嘗試的選擇器input[type=text]:focus+fieldsetfieldset元素相匹配,立即跟隨聚焦的文本輸入框 - 與您想要的東西完全不同。

然而,使用JavaScript處理這個問題可能並且相當容易。你只需要在fieldset中的字段上使用onfocus和onblur事件處理程序,而這些處理程序可以是所有這些處理程序的相同功能;他們只是改變了fieldset元素的style.background財產,

+0

這就是我懷疑的。好,謝謝! – 2011-12-29 01:03:58

0

,那麼就做這樣的事情:

http://www.pmob.co.uk/temp/categorybox.htm

如果你需要邊界和可訪問性,可以考慮在div中包裝fieldset,然後設計包含div而不是字段集的樣式。

希望這會有所幫助!

馬特

+0

感謝馬特,我就檢查了這一點了。 :) – 2011-12-29 03:07:27

0

如果您正在使用jQuery和你是不是嵌套的字段集,你可以做一個簡單的綁定其自身附加到你的所有頁面控件字段集內,每當您將重點放在/關注這些控件中的任何一個,從包含該控件的字段集中添加/移除一個類。

這裏有一個例子:

$('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'); }); });

6

基於其子input S的一個焦點狀態時,無法風格fieldset

但是,您可以通過添加一個空divfieldset的最後一個孩子,其樣式,模擬效果。這div的風格,然後可以使用一般的兄弟選擇來改變所聚焦的input

fieldset { 
 
    border: none; 
 
    position: relative; 
 
    margin-bottom: 0.5em; 
 
} 
 

 
legend { 
 
    position: relative; 
 
    background: white; 
 
} 
 

 
input:focus { 
 
    background: lightyellow; 
 
} 
 

 
input:focus ~ div { 
 
    border: 2px solid black; 
 
    background: #def; 
 
} 
 

 
fieldset > div { 
 
    height: calc(100% - 0.5em); 
 
    width: 100%; 
 
    position: absolute; 
 
    top: 0.5em; 
 
    left: 0; 
 
    border: 2px solid lightgray; 
 
    z-index: -1; 
 
}
<fieldset> 
 
    <legend>Fieldset 1</legend> 
 
    <input name="text1" type="text" /> 
 
    <input name="text2" type="text" /> 
 
    <div></div> 
 
</fieldset> 
 
<fieldset> 
 
    <legend>Fieldset 2</legend> 
 
    <input name="text3" type="text" /> 
 
    <input name="text4" type="text" /> 
 
    <div></div> 
 
</fieldset>

+0

哈,聰明的黑客 - 偉大的普通背景。我期望這樣的事情。謝謝! – 2015-04-05 04:55:15

+1

當然,對不起,我的速度不夠快,不能在問題關閉前發帖。 – 2015-04-05 04:56:12

相關問題