仅当特定元素位于您要选择的元素之后时,如何使用CSS选择元素? [英] How to use CSS to select an element only if a specific element comes after the one you want to select?

查看:88
本文介绍了仅当特定元素位于您要选择的元素之后时,如何使用CSS选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CSS和下面的示例,有没有办法只选择<p>,然后选择<ul>?

Using CSS and the following example, is there a way to select only <p>'s that are followed by <ul>'s?

如果不是,那么jQuery选择器是什么,或者它需要if语句?

If not, what is the jQuery selector, or would it require an if statement?

<p>Hello there.</p>

<p>Select this para!</p>
<ul>
    <li>list item</li>
</ul>

<ul>
    <li>don't select this list! :)</li>
</ul>

<p>And don't select this paragraph! :)</p>

推荐答案

在CSS3中是不可能的,最好的办法是仅选择<p>之后的<ul>:

It's not possible in CSS3, the best you can do is select only the <ul> which follow <p>:

p + ul { /*...*/ }

但是,当浏览器开始实现 CSS4主题运算符时,:

However it will be possible when browsers start implementing the CSS4 subject operator:

!p + ul { /*...*/ }

同时,您将不得不使用jQuery并返回DOM.

In the meantime, you'll have to use jQuery and walk the DOM back.

这篇关于仅当特定元素位于您要选择的元素之后时,如何使用CSS选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆