简体   繁体   English

CSS选择器是否有以下兄弟姐妹

[英]Is there a following sibling for CSS selectors

I am very new with CSS selectors and was wondering if there was something like a following sibling for them.我对 CSS 选择器很陌生,想知道是否有类似以下兄弟姐妹的东西。

Example: I am trying to extract just the dates from this piece of code示例:我试图从这段代码中提取日期

<p class="premiumOnly">
<strong>Original bar admit date:</strong> 5/10/1974<br>
<strong>Colorado bar admit date:</strong> 5/10/1974

The selector #Listing > div.col-sm-8.text-left.memberOnly > div.row > div.col-sm-7 > p:nth-child(4) > strong:nth-child(1) is only getting the text Original bar admit date but not the actual dates选择器#Listing > div.col-sm-8.text-left.memberOnly > div.row > div.col-sm-7 > p:nth-child(4) > strong:nth-child(1)只是获取文本原始酒吧承认日期但不是实际日期

Kindly advise if there is a way to get the dates only.请告知是否有办法仅获取日期。 I can also accommodate a Regex along with the selector if needed如果需要,我还可以容纳正则表达式和选择器

Thanks谢谢

您必须用一些span或其他标签包装要设置样式的文本才能选择它。

Targeting strong:nth-child(1) will just grab what's in the strong tag.定位strong:nth-child(1)只会获取 strong 标签中的内容。 I'm assuming p:nth-child(4) is .premiumOnly in your code.我假设p:nth-child(4)在您的代码中是.premiumOnly To extract just the dates, you can target that paragraph, and use regex and remove the stuff you don't want.要仅提取日期,您可以定位该段落,然后使用正则表达式并删除您不想要的内容。

 var p = document.getElementById('p').innerHTML; p = p.replace(/<strong>.*<\\/strong>|<br>|\\n/g,''); console.log(p);
 <p class="premiumOnly" id="p"> <strong>Original bar admit date:</strong> 5/10/1974<br> <strong>Colorado bar admit date:</strong> 5/10/1974 </p>

If your actual use-case is just as your example is, you should be able apply a style to the entire paragraph (dates and strong text), and then remove that style from the strong text.如果您的实际用例与您的示例一样,您应该能够将样式应用于整个段落(日期和强文本),然后从强文本中删除该样式。 What you're left with is a style that only affects the dates.剩下的是只影响日期的样式。

For example, if you wanted to make the dates red... https://jsfiddle.net/1sdqoug5/例如,如果您想让日期变成红色... https://jsfiddle.net/1sdqoug5/

 p.premiumOnly { color: red; } p.premiumOnly > strong { color: initial; }
 <p class="premiumOnly"> <strong>Strong Text: </strong> date<br> <strong>Strong Text 2: </strong> date </p>

It is exists indeed, the selector A + B matches any B element preceded by an A element.它确实存在,选择器A + B匹配前面有A元素的任何B元素。 It is not of help in you case however, because the strong element has no immediate siblings.但是,这对您没有帮助,因为strong元素没有直接的兄弟姐妹。 The date you want to extract is a text node.您要提取的日期是一个文本节点。 You either wrap it in a span like <strong>Original bar admit date:</strong> <span>5/10/1974</span so you can match it by selecting strong + span您可以将其包装在一个span例如<strong>Original bar admit date:</strong> <span>5/10/1974</span以便您可以通过选择strong + span来匹配它

or alternatively use the DOM or jQuery API to target the text node或者使用 DOM 或 jQuery API 来定位文本节点

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

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