最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery check if element exists then add class to different element - Stack Overflow

programmeradmin8浏览0评论

I don't know much about jquery at all, so bear with my ignorance, but I'm pretty sure I can acplish this with jquery.

I need jquery to check if an element exists, and if so, add a class to a different element. For example,

if class "minimal-price-link" exists, then add a class to "regular-price" or what I really am wanting to do is make the regular-price strikethrough.

Here's the code:

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

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

I don't know much about jquery at all, so bear with my ignorance, but I'm pretty sure I can acplish this with jquery.

I need jquery to check if an element exists, and if so, add a class to a different element. For example,

if class "minimal-price-link" exists, then add a class to "regular-price" or what I really am wanting to do is make the regular-price strikethrough.

Here's the code:

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

   <a href="http://stemulation./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>
Share Improve this question edited Apr 29, 2010 at 4:25 Mitch Dempsey 40k6 gold badges71 silver badges75 bronze badges asked Apr 29, 2010 at 4:23 BuddyBuddy 211 gold badge1 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

this should do it...

$(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

if($('.minimal-price-link').length != 0)
{
  $('.regular-price').addClass('strikethrough');
}

If any element has the minimal-price-link class, add the strikethrough class to all elements with the regular-price class. This may not be exactly what you want, but it should give you the idea.

if ($("a.minimal-price-link").length > 0)
{
    $("span.regular-price").addClass("yourclass");
}

<style type="text/css">
    .yourclass { text-decoration: line-through; }
</style>
发布评论

评论列表(0)

  1. 暂无评论