简体   繁体   English

访问子元素时更改父项的颜色

[英]Changing the colour of parent item when child element is visited

I'm trying to stylize a horizontal menu item with several child items. 我试图用几个子项来风格化水平菜单项。 My idea was to change the colour of the parent item and the drop down menu as well when a child item of the menu is visited. 我的想法是在访问菜单的子项时更改父项和下拉菜单的颜色。 I'm not so familiar with javaScript that's whY I want to ask for an opinion - is it possible to do this only in CSS approach ? 我对javaScript不太熟悉,所以我想征求意见-仅在CSS方法中可以做到这一点吗?

Here is my HTML: 这是我的HTML:

<ul class="gf-menu l1">
<li class="item128 parent">
 <a class="item" href"services">Services<span class="border-fixer"></span>::after</a>
<div class="dropdown columns-1">
<div class="column col1">
<ul class="l2">
<li class ="item1"><a class="item" href="submenu-01">Submenu1</a></li>
<li class ="item2"><a class="item" href="submenu-02">Submenu2</a></li>
<li class ="item3"><a class="item" href="submenu-03">Submenu3</a></li>
</ul>
</div>
</div>
</li>

And the CSS: 和CSS:

.gf-menu .dropdown{
border: 1px solid transparent; 
border-radius:0; 
background-color:#a9a9a9; 
padding:10% 0; 
width:100%;
text-shadow:none;
font-size:85%;
.gf-menu.l1 li.item1.active.last {background-color:#abcf39;}
.gf-menu.l1 li.item2.active.last {background-color:#f39512;}
.gf-menu.l1 li.item3.active.last {background-color:#f16e68;}

Any help would be appreciated, thank you in advance! 任何帮助将不胜感激,在此先感谢您!

You cannot target a parent of an element with css yet. 您尚不能使用CSS定位元素的父级。 css selectors level 4 (CSS4) makes this possible by using the "!" css选择器级别4(CSS4)通过使用“!”使其成为可能。 on the operand will come out with this feature. 在操作数上将带有此功能。

What you can do is the opposite. 相反,您可以做的是。 You can use the pseudo "visited" to target the chile element and give it a certain color 您可以使用伪造的“已访问”来定位智利元素并为其指定特定颜色

.parent:visited .child {
    Attributes come here
}

Using jQuery you can target the parent with ".parent()" I recommend using that approach for your menu. 使用jQuery,您可以使用“ .parent()”定位父对象。我建议您在菜单中使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM