[英]CSS selector that matches parent of empty element
I have such HTML code 我有这样的HTML代码
<li data-bind="attr: { class: MenuItem.CssClass, 'data-Id': MenuItem.Id }">
<div class="subMenuActions">
<i class="icon-caret-right" />
<i class="icon-caret-down" />
</div>
...
<ul class="subMenu"></ul>
</li>
I want to make selector for elements i
in div.subMenuActions
which will check if ul.subMenu
is empty. 我想在div.subMenuActions
为元素i
做选择器,这将检查ul.subMenu
是否为空。
I know that I can check if ul.subMenu
is empty by using CSS selector ul.subMenu:empty
, but I need to hide i.icon-caret-down
, i.icon-caret-right
if ul.subMenu
is empty. 我知道,我可以检查是否ul.subMenu
是通过使用CSS选择器空ul.subMenu:empty
,但我需要隐藏i.icon-caret-down
, i.icon-caret-right
如果ul.subMenu
是空的。
Please no JS answers. 请没有JS答案。
There is no way to select previous element sibling nor parent element with CSS on levels 1-3. 无法使用1-3级的CSS选择先前的元素同级或父元素 。 As Mishik say, you can only select next siblings. 正如Mishik所说,您只能选择下一个兄弟姐妹。
In your case if you reorder elements in HTML and you know the height of the ul.submenu
element, you can use this solution: 就您而言,如果您对HTML中的元素进行重新排序并且知道ul.submenu
元素的高度,则可以使用以下解决方案:
<li data-bind="attr: { class: MenuItem.CssClass, 'data-Id': MenuItem.Id }">
<ul class="subMenu"></ul>
<div class="subMenuActions">
<i class="icon-caret-right"/>
<i class="icon-caret-down"/>
</div>
...
</li>
then apply this css: 然后应用此CSS:
li {
position:relative;
padding-bottom:2em;
}
ul.subMenu {
height:2em;
position:absolute;
width:100%;
bottom:0;
}
div.subMenuActions {
position:relative;
}
ul, div { /* normalize to zero */
margin:0;
padding:0;
}
/* and the selectors */
ul.subMenu:empty + .subMenuActions > i,
ul.subMenu:empty + .subMenuActions + .subMenuActions > i {
display: none;
}
http://jsfiddle.net/7VmR9/15/ http://jsfiddle.net/7VmR9/15/
All in all, much easier way here to go is to use javascript. 总而言之,这里更简单的方法是使用javascript。
There is no "previous sibling" selector in CSS, unfortunately (at least for now). 不幸的是(至少到目前为止),CSS中没有“先前的同级”选择器。
I could only suggest changing the order of .subMenu
and .subMenuActions
and then use this: 我只能建议更改.subMenu
和.subMenuActions
的顺序,然后使用以下命令:
.subMenu:empty + .subMenuActions > i{
display: none;
}
Afterall, you can always style .subMenuActions
to display it in the required place. 毕竟,您始终可以设置.subMenuActions
样式以在所需位置显示它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.