单击图像可以添加和删除多个Div的类

时间:2017-03-22 00:53:34

标签: javascript jquery html css

我有四种颜色显示为带图像的单选按钮。如果单击彩色图像,我试图获取彩色图像ID并将其作为类添加到多个div元素。

它正在工作,但它只是在点击图像时添加每个图像的类(基本上附加),我想要的是如果单击图像只添加该图像的ID作为类,如果一个单击更多图像,删除现有图像类和新图像类。

这是我的代码

HTML

<ul id="thumbnails1" class="noul swatch-colors">
<li><label class="lbswatches"><input class="rcolor" name="swatch_color" value="Cherry Red" type="radio"><img src="https://image.ibb.co/mZnrqa/g500_antique_cherry_red_color.jpg" id="CherryRed" class="full swatch-img" alt="Cherry Red" title="Cherry Red" width="32" height="32"></label></li>
<li><label class="lbswatches"><input class="rcolor" name="swatch_color"  value="Irish Green" type="radio"><img src="https://image.ibb.co/cuJLiv/g500_antique_irish_green_color.jpg" id="IrishGreen" class="full swatch-img" alt="Irish Green" title="Irish Green" width="32" height="32"></label></li>
<li><label class="lbswatches"><input class="rcolor" name="swatch_color" value="Jade Dome" type="radio"><img src="https://image.ibb.co/eVABqa/g500_antique_jade_dome_color.jpg" id="JadeDome" class="full swatch-img" alt="Jade Dome" title="Jade Dome" width="32" height="32"></label></li>
<li><label class="lbswatches"><input class="rcolor" name="swatch_color" value="Orange" type="radio"><img src="https://image.ibb.co/jGR6Ov/g500_antique_orange_color.jpg" id="Orange" class="full swatch-img" alt="Orange" title="Orange" width="32" height="32"></label></li>
</ul>
<h2 id="colorselected"></h2>
<br /><br /><br />
<div class="adult-section-box">
 <div class="pc-row">
 <ul class="quote-sizes adult-sizes noul pc-col">
<li><label><span>Adult Small</span><input id="adult_s" type="text" name="adult_s" data-var="adult_s" class="amtbox" placeholder="quanity" /></label></li>
      <li><label><span>Adult Medium</span><input id="adult_m" type="text" name="adult_m" data-var="adult_m" class="amtbox" placeholder="quanity" /></label></li>
      <li><label><span>Adult Large</span><input id="adult_l" type="text" name="adult_l" data-var="adult_l" class="amtbox" placeholder="quanity" /></label></li>
    </ul>
 </div>
 </div>
<br /><br /><br />
<div class="youth-section-box">
 <div class="pc-row">
 <ul class="quote-sizes youth-sizes noul pc-col">
 <li><label><span>Youth X Small</span><input id="youth_xs" type="text" name="youth_xs" data-var="youth_xs" class="amtbox" placeholder="quanity" /></label></li>
      <li><label><span>Youth Small</span><input id="youth_s" type="text" name="youth_s" data-var="youth_s" class="amtbox" placeholder="quanity" /></label></li>
      <li><label><span>Youth Medium</span><input id="youth_m" type="text" name="youth_m" data-var="youth_m" class="amtbox" placeholder="quanity" /></label></li>
    </ul>
 </div>
 </div>

的jQuery

document.querySelectorAll('[name=swatch_color]')[0].checked = true;
    $('#thumbnails1').delegate('img','click', function(){
         $(this).closest('ul').find('.rcolor').attr('checked',false);
       $(this).prev().attr('checked',true);
       $("#colorselected").val($(this).attr("alt"));
       var imgid = this.id;
        $(".adult-section-box .pc-row, .youth-section-box .pc-row").addClass(imgid);           
   });

这是我的jFiddle demo

由于

2 个答案:

答案 0 :(得分:1)

您可以使用div的jQuery#attr属性重置所有这些内容。

    $(".full.swatch-img").on('click',function(){

        let colorCode = $(this).attr('id');

        $(".adult-section-box .pc-row, .youth-section-box .pc-row")
        .attr('class',colorCode+" pc-row");

    })

更新了fiddle

答案 1 :(得分:0)

而不是

.addClass(imgid)

你可以使用

.attr("class",imgid);

这将覆盖元素的class属性 所以在你的情况下,你想要:

$(".adult-section-box .pc-row, .youth-section-box .pc-row").attr("class",imgid);