目标元素在其他元素之前 [英] Target element right before other element

查看:48
本文介绍了目标元素在其他元素之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对此进行研究期间,我偶然发现了 此线程 ,但是由于它已有2年的历史了,我想知道是否有一种针对某个元素的最新方法.

例如:

 < span>Div</span>之前的元素< div id ="thediv">使用#thediv</div>从此处定位目标范围 

是否有任何 CSS 可以完成此任务?
我知道 jQuery的用法,如果普通CSS可以实现,我宁愿不使用.

谢谢.

阐明用法的示例.

 < ul>< li>< span> SubTitle Span</span>< div class ="subdiv"> SubTitle</div>之后的一些内容;</li>< li>< span>项目说明</span>< div>项目内容</div></li>< li>< span>项目说明</span>< div>项目内容</div></li>< li>< span> SubTitle Span</span>< div class ="subdiv"> SubTitle</div>之后的一些内容;</li>< li>< span>项目说明</span>< div>项目内容</div></li>< li>< span>项目说明</span>< div>项目内容</div></li>< li>< span> SubTitle Span</span>< div class ="subdiv"> SubTitle</div>之后的一些内容;</li>< li>< span>项目说明</span>< div>项目内容</div></li></ul> 

解决方案

没有,单独使用CSS不可能做到这一点

如果您的HTML的结构如下所示,那么可以使用CSS

 < div id ="thediv">使用#thediv</div>从此处定位目标范围< span>Div</span>之前的元素 

例如,要使用div更改跨度的颜色,您将写为

  #thediv + span {颜色:蓝色;} 

这称为相邻的同级选择 r.

CSS中没有办法定位上一个元素,但是可以像上面显示的那样定位下一个元素.

During my research for this, I stumble upon this thread, but since it is 2 years old, Im wondering if there's a recent way of targeting an element that is right before another.

For example:

<span> Element Before Div </span>
<div id="thediv"> Target Span From Here Using #thediv </div>

Is there any CSS that can accomplish this?
I'm aware of the usage of jQuery, I rather not use it if this is possible with plain CSS.

Thank you in advance.

EDIT: Example to clarify usage.

  <ul>
     <li>
        <span>SubTitle Span</span>
        <div class="subdiv">Some content after SubTitle</div>
     </li>
     <li>
        <span>Item Description</span>
        <div>Item Content</div>
     </li>
     <li>
        <span>Item Description</span>
        <div>Item Content</div>
     </li>
     <li>
        <span>SubTitle Span</span>
        <div class="subdiv">Some content after SubTitle</div>
     </li>
     <li>
        <span>Item Description</span>
        <div>Item Content</div>
     </li>
     <li>
        <span>Item Description</span>
        <div>Item Content</div>
     </li>
     <li>
        <span>SubTitle Span</span>
        <div class="subdiv">Some content after SubTitle</div>
     </li>
     <li>
        <span>Item Description</span>
        <div>Item Content</div>
     </li>
  </ul>

解决方案

No this is not possible using CSS alone

if your HTML had been structured like below then yes CSS can be used

<div id="thediv"> Target Span From Here Using #thediv </div>
<span> Element Before Div </span>

for e.g to change the color of the span using the div, you would write as

#thediv + span {
color:blue;
}

this is called adjacent sibling selector.

There is no way in CSS to target a previous element ,but a following element can be targeted like shown above.

这篇关于目标元素在其他元素之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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