Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery检查元素是否存在,然后将类添加到不同的元素中_Javascript_Jquery - Fatal编程技术网

Javascript Jquery检查元素是否存在,然后将类添加到不同的元素中

Javascript Jquery检查元素是否存在,然后将类添加到不同的元素中,javascript,jquery,Javascript,Jquery,我对jquery了解不多,所以请容忍我的无知,但我非常确信我可以用jquery完成这一点 我需要jquery检查元素是否存在,如果存在,则向其他元素添加一个类。比如说, 如果类“最小价格链接”存在,那么在“常规价格”中添加一个类,或者我真正想做的是使常规价格删除线 代码如下: <div class="price-box"> <span class="regular-price" id="product-price-2"> <span class=

我对jquery了解不多,所以请容忍我的无知,但我非常确信我可以用jquery完成这一点

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

如果类“最小价格链接”存在,那么在“常规价格”中添加一个类,或者我真正想做的是使常规价格删除线

代码如下:

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

$240.00
如果任何元素具有最小价格链接类,则将删除线类添加到具有常规价格类的所有元素中。这可能不是您想要的,但它应该会给您一个想法。

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

<style type="text/css">
    .yourclass { text-decoration: line-through; }
</style>
{ 美元(“span.常规价格”).addClass(“yourclass”); } .yourclass{文本装饰:线穿过;}
这应该可以

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

});​
$(函数(){
$(“a.minimal-price-link”)。每个(函数(){
//用罢工来包装它是很容易的
$(this).closest('.price box').find(“span.regular price”).wrap(“”);
//或者您也可以添加一个类
//$(此).最近('.price box').find(“span.regular price”).addClass(“strike”);
})
});​

我想你忘了在
最小价格链接之后添加一个'a
如果只有一个“最小价格链接”,这个脚本会在所有“常规价格”跨度中添加一个“删除线”类。真是太棒了!谢谢你的帮助…它工作得很好。我真的很喜欢Reigel的演示。我必须一劳永逸地学习jquery。继续努力。我认为如果存在一个“最小价格链接”类,那么这段代码会将“yourclass”添加到所有常规价格范围中。谢谢Reigel。它工作得很好。仅对具有特殊价格的产品添加删除线。没有特价的产品通常都是按原样设计的。完美的
$(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");
  })

});​