我试图在单击时将 div id 添加到 jquery 数组中。在应用“选定”css 类的 div 上也有一个切换。I only want to add id's to the div when the selected css class is active on the div. 除此之外,我在数组中遇到重复问题。我以为我正确使用了独特的功能,但我想不是。
这是我正在使用的 HTML:
<div class="content">
<a href="#item3" class="panel multi"><div class="circle skinCare multi" id="acne" style="top:10px;left:265px"><div class="inner_circle" id="acne">ACNE/</br>BLEMISHES</div></div></a>
<a href="#item3" class="panel multi"><div class="circle skinCare multi" id="dry_skin" style="top:255px;left:10px"><div class="inner_circle" id="dry_skin">DRY SKIN</div></div></a>
<a href="#item3" class="panel multi"><div class="circle skinCare multi" id="oily_skin" style="top:90px;left:100px"><div class="inner_circle" id="oily_skin">OILY SKIN/LARGE PORES</div></div></a>
<a href="#item3" class="panel multi"><div class="circle skinCare multi" id="aging" style="top:90px;left:430px"><div class="inner_circle" id="aging">WRINKLES/</br>AGING</div></div></a>
<a href="#item3" class="panel multi"><div class="circle skinCare multi" id="irritation" style="top:255px;left:510px"><div class="inner_circle" id="irritation">SENSATIVE SKIN/IRRITATION</div></div></a>
<a href="#item3" class="panel multi"><div class="circle skinCare multi" id="dark_spots" style="top:430px;left:100px"><div class="inner_circle" id="dark_spots">DARK SPOTS</div></div></a>
</div>
这是jQuery:
var skinCare=[];
$('.skinCare').click(function(){
//var index = "Skin Care";
if($('div.skinCare').css('background-color') == 'rgb(255, 0, 0)'){
skinCare.push(event.target.id);
}
var skinCare1 = $.unique(skinCare);
alert(skinCare1);
});
它将其输出到警报中:
acne, aging, acne, acne, irritation
如果选择了某些东西,我需要它向数组添加一个 id,如果它被取消选择,我需要它从数组中删除它。