簡體   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