jquery - 使用 CSS 定位文本但不定位同级元素

标签 jquery html css css-selectors

在下面的示例中,我尝试定位“$59.00”

<div class="ProductPriceRating">
    <em>
        <strike class="RetailPriceValue">$69.00</strike> 
        $59.00
    </em>
</div>

我对此有点困惑。

最佳答案

除非您愿意更改 DOM 或覆盖,否则您无法执行此操作。

因此,您需要使用 span 元素来包装文本,而不是像这样的目标

div.ProductPriceRating > em > span {

}

否则,如果您无法更改 DOM,则需要使用两个选择器,第一个选择器会将样式应用于 em,下一个选择器将使用 strike 覆盖该样式

Demo

div.ProductPriceRating > em {
    color: red;
}

div.ProductPriceRating > em > strike {
    color: black;
}
<小时/>

您可以使用的另一种方式是:not,但如果您使用color,那么继承会给您带来问题

Demo

div.ProductPriceRating em:not(.RetailPriceValue) {
    background: red;
}
<小时/>

正如您所评论的,您愿意使用 jQuery,那么我们正在使用 jQuery 包装节点..

$(".ProductPriceRating em").contents().filter(function() {
    return this.nodeType != 1; //or return this.nodeType == 3;  <-- Preferable 
}).wrap("<span></span>");

Demo

关于jquery - 使用 CSS 定位文本但不定位同级元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546051/

相关文章:

javascript - jQuery UI 可排序链接元素

html - 向下滚动时背景剪切

ios - 如何在响应所有设备的 native 应用程序中设置 marginLeft 和 marginRight

iphone - iphone 上使用 font-face 的不同行高

jQuery : Inserting into newly created elements

javascript - jQuery append 不断重复如何只保留一个

c# - 如何重置 DropDownList 索引

javascript - 渐进式WA : Do we have to add links to manifest to the header section of all website pages?

html - 为什么 ul 和第一个 li 元素之间有一个空格?

javascript - 让段落行自下而上而不是自上而下的技巧