gpt4 book ai didi

javascript - Jquery 检查元素是否存在然后将类添加到不同的元素

转载 作者:行者123 更新时间:2023-11-29 16:28:52 25 4
gpt4 key购买 nike

我对 jquery 知之甚少,所以请原谅我的无知,但我很确定我可以用 jquery 完成这件事。

我需要 jquery 来检查元素是否存在,如果存在,则将类添加到不同的元素。例如,

如果类“minimal-price-link”存在,则将类添加到“regular-price”或者我真正想做的是使 regular-price 删除线。

代码如下:

<div class="price-box">
<span class="regular-price" id="product-price-2">
<span class="price">$240.00</span>
</span>

<a href="http://stemulation.com/order/sfs30.html" class="minimal-price-link">
<span class="label">Your Price:</span>
<span class="price" id="product-minimal-price-2">$110.00</span>
</a>
</div>

最佳答案

这应该能做到...

$(function() {

$("a.minimal-price-link").each(function(){

// it would be easy to wrap it with strike
$(this).closest('.price-box').find("span.regular-price").wrap("<s>");

// or you can also add a class
// $(this).closest('.price-box').find("span.regular-price").addClass("strike");
})

});​

demo

关于javascript - Jquery 检查元素是否存在然后将类添加到不同的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2734757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com