CSS 为新元素创建选择器

CSS create a selector for a new element

这是一个棘手的问题,我需要 select class="price_code" 中的元素,但它只需要在 data-product-id 等于 447 时适用。如何使用 CSS 完成此操作?还是需要 Javascript 解决方案?

<span class="price_code" data-product-id="447">
   <span class="Price-amount amount">
      <span class="Price-currencySymbol">£</span>&nbsp;0
   </span>
</span>

我知道通常您只需使用 CSS 将 .price_code 指定为 select 和 class,但是我如何指定 data-product-id需要 447?

您可以使用 attribute selector:

.price_code[data-product-id="447"] {
  color: red;
}
<span class="price_code" data-product-id="447">
   <span class="Price-amount amount">
      <span class="Price-currencySymbol">£</span>&nbsp;0
</span>
</span>

<span class="price_code" data-product-id="448">
   <span class="Price-amount amount">
      <span class="Price-currencySymbol">£</span>&nbsp;1
</span>
</span>