繁体   English   中英

jQuery检查元素是否存在,然后将类添加到其他元素

[英]Jquery check if element exists then add class to different element

我对jquery一点都不了解,所以请忍受我的无知,但是我敢肯定我可以使用jquery完成此工作。

我需要jquery检查元素是否存在,如果存在,则将一个类添加到其他元素。 例如,

如果存在“ minimum-price-link”类,则将一个类添加到“ 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");
  })

});​

演示

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

如果任何元素具有minimum-price-link类,则将删除线类添加到所有带有Regular-price类的元素中。 这可能不完全是您想要的,但是它应该可以为您提供想法。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM