切换当前活动父元素

时间:2015-07-09 15:24:23

标签: jquery css

现在我有三个复选框 - 这是一个如何设置部分的示例:

<div class="color-swatches">
      <div class="swatch">
        <img src="/img/Apothecary_Swatch.jpg" class="swatch-icon image">
        <p class="color-name">Apothecary</p>
        <div class="check off"></div>
      </div>
      <div class="swatch">
        <img src="/img/Arbor_Swatch.jpg" class="swatch-icon image">
        <p class="color-name">Arbor</p>
        <div class="check off"></div>
      </div>
      <div class="swatch">
        <img src="/img/Bubble_Swatch.jpg" class="swatch-icon image">
        <p class="color-name">Bubbles</p>
        <div class="check off"></div>
      </div>
      <div class="swatch">
        <img src="/img/TransparentSpacer.gif" class="swatch-icon image">
        <p class="color-name">Classic</p>
        <div class="check off"></div>
      </div>
      <div class="swatch">
        <img src="/img/LadyBug_Swatch.jpg" class="swatch-icon image">
        <p class="color-name">Lady Bug</p>
        <div class="check off"></div>
      </div>
      <div class="swatch">
        <img src="/img/Burst_Swatch.jpg" class="swatch-icon image">
        <p class="color-name">Starburst</p>
        <div class="check off"></div>
      </div>

.color-swatches包含部​​分的所有复选框。现在,我设置了一些内容,以便您可以单击图像(具有类.swatch-icon),而具有类.check的div将替换.off类。在一个(.off有一个非活动复选框的图像,.on有一个活动的图像)。

我需要做的是确保每个.color-swatches div只有一个swatch-icon被选中&#34; (换句话说,对于一个.check div,一次可以有.on个类。现在,我可以在.color-swatches div中选择任何或所有样本,这不是我想要的。我该怎么做才能改变我的jQuery以使这个功能发生?

这是我目前的jQuery:

(function($) {

    $(document).ready(function() {
      $('.color-swatches').each(function() {

         $('.swatch').each(function(i, n) {

            $(n).find('.swatch-icon').bind('click', function(e) {
                e.preventDefault();

                var currentCheck = $(this).find('.check');

                if(currentCheck.hasClass('off')){
                  currentCheck.removeClass('off');
                  currentCheck.addClass('on');
                }

            }.bind($(this)));

         });

    });

  });
}(jQuery));

1 个答案:

答案 0 :(得分:4)

您可以通过将类添加到已单击的元素并将其从所有其他select Id = IDENTITY(8) , t2.val op , t1.val obj into existing table #l from #t1 t1, #t1 t2 where t1.col = 'ObjectId' and t1.val = 'A' -- this would be the cursors value and t1.rownr = t2.rownr and t2.col = 'Operation' 元素中删除来大规模简化代码。试试这个:

select * from #l order by 3,1
 Id          op     obj  
 ----------- ------ -----
           1 Op1    A    
           2 Op2    A    
           1 Op3    B    
           2 Op4    B    
           3 Op5    B    

Example fiddle