使用:has将类添加到div的特定实例

时间:2017-03-17 12:49:37

标签: javascript jquery

我试图将一个类添加到一个div(或多个,取决于搜索结果返回的内容)但是我在下面的代码是将类添加到所有.price.full div上页面,而不仅仅是应用于与:has匹配的那个页面。

jQuery(document).ready(function() {
    jQuery('.price.full p.price:has(small)').closest('.price.full').addClass('price-enquiry');
});

HTML

    <div class="price-rating">

    <span class="price full price-enquiry">
        <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">

            <p class="price"><small>+44 (0) 1395<br> 444099</small></p>

            <meta itemprop="price" content="0">
            <meta itemprop="priceCurrency" content="GBP">
            <link itemprop="availability" href="http://schema.org/InStock">

        </div>

    </span>
    <div class="product-codes">
        <span class="oe-code">
            OE Code: 82-5323/4          
        </span><!-- .oe-code -->
        <span class="sku">
            SKU: MG41           
        </span>
    </div><!-- .product-codes -->
</div>

1 个答案:

答案 0 :(得分:-2)

:has(small),你的意思是:has(.small)

您的选择器显示您在p.price中寻找的.price.full有一个标记为small的元素。

我同意其他人在显示完整的HTML以获得正确的答案。