html - 使用 Css 显示特定条件下的文本

标签 html css xml

鉴于我无法更改下面的 Xml,是否可以仅使用一个 css 文件获得条件结果。我目前只能使用一个 css,但据我了解,css 在条件方面非常有限。

案例 1:

<doc>
<story>
<c1><p><text-web-group class="MajorEvent"><text-web class="MajorEvent">Sample Text C1</text-web></text-web-group></p></c1>
</story>
</doc>

案例 2:

<doc>
<story>
<c1><p><text-web-group class="MajorEvent"><text-web class="MajorEvent">Sample Text C1</text-web></text-web-group></p></c1>
<c2><p><text-web-group class="MajorEvent"><text-web class="MajorEvent">Sample Text C2</text-web></text-web-group></p></c2>
</story>
</doc>

案例 3

<doc>
<story>
<c2><p><text-web-group class="MajorEvent"><text-web class="MajorEvent">Sample Text C2</text-web></text-web-group></p></c2>
</story>
</doc>
  • CASE 1. 如果只有 C1/p/text-web-group/text-web 中有文本,则显示 Sample Text C1
  • CASE 2. 如果 C1/p/text-web-group/text-web 和 C2/p/text-web-group/中都有文本,则只显示 Sample Text C1文本网络
  • 案例 3. 如果仅在 C2/p/text-web-group/text-web 中有文本,则显示 示例文本 C2

最佳答案

是的,有一种方法只使用 CSS。根据您的解释,您可以使用以下 CSS:

story > *:first-child + * > p > text-web-group{
    display: none;
}

Working Fiddle (更改 html 以检查)

PS:如果你有更多的标签,如c1c2c3 等,那么使用~ (sibling selector) 而不是上面代码中的 + (adjacent selector)。

注意:使用通用选择器 *,就像我所做的一样 effect the performance .不在 modern browsers虽然。

关于html - 使用 Css 显示特定条件下的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25236897/

相关文章:

c# - 使用 XmlReader 读取属性值

javascript - 如何从iframe中的文档对象获取父元素

html - CSS3 动画滞后

JavaScript .隐藏('.var:after')

java : finding absolute xpath of specific node

sql - 如何从 Oracle 中获取格式化的 XML

html - 如何使滚动列表中的 anchor 扩展到其子项的宽度?

html - 使用 z-index 堆叠伪元素

html - 垂直居中 <a> 标签?

html - Horizo​​ntal Scrolling with Vertical Navbar - 向后滚动时导航栏下的内容