Javascript 使用:has将类添加到div的特定实例
我试图将一个类添加到一个div或多个div中,这取决于搜索结果返回的内容。然而,我下面的代码将该类添加到页面上的所有.price.full div中,而不仅仅是应用到与:has匹配的div中 HTMLJavascript 使用:has将类添加到div的特定实例,javascript,jquery,Javascript,Jquery,我试图将一个类添加到一个div或多个div中,这取决于搜索结果返回的内容。然而,我下面的代码将该类添加到页面上的所有.price.full div中,而不仅仅是应用到与:has匹配的div中 HTML <div class="price-rating"> <span class="price full price-enquiry"> <div itemprop="offers" itemscope="" itemtype="http
<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>
字体hassmall,你的意思是:has.small
您的选择器显示您在.price.full中查找具有标记名为small的元素的p.price
我同意其他人关于显示完整html以获得正确答案的看法。您的jQuery看起来不错。我们需要查看HTML以诊断您询问的有关DOM选择的问题,但没有显示页面的外观。很难帮上忙。小元素在哪里?当您询问客户端DOM选择时,为什么要发布PHP?现在您已经没有了。您需要在发布之前验证您的示例是否确实代表了问题,而不仅仅是在发布中抛出一些代码。即使是现在,也没有任何问题。它如何帮助我们看到一个正常工作的元素,而不是给我们带来麻烦的元素?small是一个有效的dom元素。因此,您可能是错的。如果没有元素,那么选择器将不会匹配任何内容,而不会匹配问题中指出的所有.price.full元素。如果没有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>