我有一個應用程序,您可以選擇任何6個divs的組合作爲選擇或不選擇(標記類active
),然後第7個div,當選擇清除所有其他。我有jQuery的工作,除了增加active
類到第七個div。這裏有一個小提琴:JQuery主動切換/添加/刪除類問題
$(document).ready(function() {
$('.q4-answer').on('click', function() {
$(this).toggleClass('active');
});
$('#q4-all').on('click', function() {
$('#q4-all').addClass('active');
$('.q4-answer').removeClass('active');
});
});
.answer {
display: inline-block;
width: 10%;
border: solid thick white;
text-align: center;
}
.answer img {
width: 30px;
}
.answer.active {
background-color: yellow;
border: solid thick black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="q4-legs" class="answer q4-answer">
<img src="http://icons.iconarchive.com/icons/iconka/meow/256/cat-grumpy-icon.png" />
<p>Legs</p>
</div>
<div id="q4-chest" class="answer q4-answer">
<img src="http://icons.iconarchive.com/icons/iconka/meow/256/cat-grumpy-icon.png" />
<p>Chest</p>
</div>
<div id="q4-back" class="answer q4-answer">
<img src="http://icons.iconarchive.com/icons/iconka/meow/256/cat-grumpy-icon.png" />
<p>Back</p>
</div>
<div id="q4-biceps" class="answer q4-answer">
<img src="http://icons.iconarchive.com/icons/iconka/meow/256/cat-grumpy-icon.png" />
<p>Biceps</p>
</div>
<div id="q4-triceps" class="answer q4-answer">
<img src="http://icons.iconarchive.com/icons/iconka/meow/256/cat-grumpy-icon.png" />
<p>Triceps</p>
</div>
<div id="q4-shoulders" class="answer q4-answer">
<img src="http://icons.iconarchive.com/icons/iconka/meow/256/cat-grumpy-icon.png" />
<p>Shoulders</p>
</div>
<div id="q4-all" class="answer q4-answer">
<img alt="A full body workout. Huzzah!" src="https://s-media-cache-ak0.pinimg.com/236x/d3/11/eb/d311eb7f9e927ea7ba604387f6278558.jpg" />
<p>Full Body</p>
</div>
誰能弄清楚爲什麼點擊後,去年有問題的div沒有得到active
類?
謝謝!這工作完美! – Liz