當焦點是另一個元素時,元素的更改樣式。 - Change style of an element when focus is another element -开发者知识库

當焦點是另一個元素時,元素的更改樣式。 - Change style of an element when focus is another element -开发者知识库,第1张

I need to change the style of a div when focus is on another div.I tried the following code but didn't work

我需要更改div的樣式,當焦點放在另一個div上時,我嘗試了以下代碼,但沒有成功。

<div id="one">
<div id="two">

my css file looks like this

我的css文件是這樣的

#one {
    background-color:red;
}
#two:focus #one {
    background-color:green;
}

Any help is really appreciated!

非常感謝您的幫助!

2 个解决方案

#1


8  

If you mean :hover and not :focus, then use adjacent selector, using which will select your #two on :hover of #one

如果您的意思是:懸浮而不是:焦點,那么使用相鄰的選擇器,使用 ,它將選擇# 2:懸浮的# 1

#one:hover   #two {
   color: red;
}

Demo

演示

Note: You've a typo, like if instead of id, also, close your div elements.

注意:您有一個錯別字,比如if而不是id,還要關閉div元素。


As you commented that you want to use focus on div, than replace :hover with :focus but the adjacent selector logic stays the same...

正如您所評論的,您希望使用focus on div,而不是replace:hover with:focus,但是相鄰的選擇器邏輯保持不變……

<div id="one" tabindex="1">One, Hover Me</div>
<div id="two">Two</div>

#one:focus   #two {
   color: red;
}

Demo (Press the first line and see the effect) OR (Click on the result window in jsfiddle, and press the tab button once)

Demo(按第一行看到效果)OR(在jsfiddle點擊結果窗口,點擊一次tab按鈕)

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复