jQuery检查元素是否存在,然后将类添加到其他元素 [英] Jquery check if element exists then add class to different element

查看:77
本文介绍了jQuery检查元素是否存在,然后将类添加到其他元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对jquery一点都不了解,所以请忍受我的无知,但是我敢肯定,我可以用jquery做到这一点.

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

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

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

如果存在"minimum-price-link"类,则将一个类添加到"regular-price"中,或者我真正想要做的是使常规价格删除线.

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.

这是代码:

<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");
  })

});​

演示

这篇关于jQuery检查元素是否存在,然后将类添加到其他元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆