Bootstrap 2.3嵌套手风琴图标也发生在父母身上

时间:2016-01-11 05:36:56

标签: jquery html css twitter-bootstrap

我正在使用bootstrap 2.3嵌套手风琴。还要编写JQuery以在打开和关闭时更改图标。但问题是,当嵌套的手风琴打开时,父母手风琴的图标也在变化。

任何人都可以帮我解决这个问题。

Sample Fiddle



$('.accordion-group').on('show', function() {
    $(this).find('.accordion-toggle').removeClass('arrow-down').addClass('arrow-up');
});    
$('.accordion-group').on('hide', function() {
    $(this).find('.accordion-toggle').removeClass('arrow-up').addClass('arrow-down');
});  

.arrow-down {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAAHwAAAB8ARUP7eQAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuNWWFMmUAAACISURBVChTvc+xCoNAEIThDXkQH8FCYhGwsRLEgJ2lEN81VQg2toIW2qVK7z9RREEtHfjg7liWOTslIZzxeJhgYhU+uOmykwQd3rpEaNEg1cMiFzyh4Rc82BU5fqjxf5xS4AstUwMtmJNBm0r4eKCHhtViMzFUTzSsGkd/W9XbrLGXO9zxeG7MBoKmGaM1wCzfAAAAAElFTkSuQmCC) no-repeat 50%;
}

.arrow-up {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAMCAYAAABr5z2BAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAbrQAAG60BIeRSlQAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAANpJREFUOE9jGJzAwsJCyM7OztPMzIwPKkQ8sLe353F2dl7v6ur6BEjPBmI1qBRxAGgAh6OjY4m7u/t/oCF/gQacBtJuUGmiAQvQC7YuLi5fgZr/Ozk5fQCyC6ByxANLS0sxoAsuAzX/dXNz+w9kzwKGDydUmijADPSSA9AVv4CG/Ae5CIhjoXL4gY2NjSjQxlKghtdQb5wGGhYClcYPgBotgQG5BqjxN9TpM4CaNaDS+AEoDQA13ABpBNr+G2hQkpWVFS9UmijABDTAEGj7DqAhIFuZIMLYAAMDAPuUQ9EUeG3QAAAAAElFTkSuQmCC) no-repeat 50%;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<script src="http://getbootstrap.com/2.3.2/assets/js/bootstrap.js"></script>

<div class="accordion" id="accordion2">
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle arrow-up" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
        Collapsible Group Item #1
      </a>
    </div>
    <div id="collapseOne" class="accordion-body collapse in">
      <div class="accordion-inner">
        <p> first table</p>
        <p>First Content</p>
        <!--inner accordion-->
        <div class="accordion" id="accordion3">
        <div class="accordion-group">
           <div class="accordion-heading">
      <a class="accordion-toggle arrow-up" data-toggle="collapse" data-parent="#accordion3" href="#collapseInner">
        Collapsible Group Item #1
      </a>
    </div>
    <div id="collapseInner" class="accordion-body collapse in">
    <div class="accordion-inner">
      Inner Accordion
      Inner Accordion
      Inner Accordion
    </div>
    </div>
        </div>
        </div>
        
      </div>
    </div>
  </div>
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle arrow-down" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
        Collapsible Group Item #2
      </a>
    </div>
    <div id="collapseTwo" class="accordion-body collapse">
      <div class="accordion-inner">
         <p> second table</p>
        <p>second Content</p>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

提前致谢

1 个答案:

答案 0 :(得分:3)

使用stopPropagation()

<强>代码:

    $('.accordion-group').on('show', function(e) {
        $(this).find('.accordion-toggle').removeClass('arrow-down').addClass('arrow-up');
        e.stopPropagation();
    });
    $('.accordion-group').on('hide', function(e) {
        $(this).find('.accordion-toggle').removeClass('arrow-up').addClass('arrow-down');
        e.stopPropagation();
    });

演示: http://jsfiddle.net/lotusgodkk/4m1u2rb4/3/