我一直在研究的是以下这段小代码:
我的目标是在单击相同元素时向标签添加active
类,并保持默认行为不变。
我不知道为什么单击事件会触发两次??
$('label.add-to-favourite').click(function(e){
alert("here");
$(this).toggleClass('active');
});
.add-to-favourite span{
cursor: pointer;
color: #d2d6de;
}
.add-to-favourite:hover span, .add-to-favourite.active span {
color: #CB3D3D;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<ul>
<li>
<label class="add-to-favourite">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-heart fa-stack-1x fa-inverse" title="Add to Favourites" data-toggle="tooltip"></i>
</span>
<input type="checkbox" name="favourites[]" value="1">
</label>
</li>
<li>
<label class="add-to-favourite">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-heart fa-stack-1x fa-inverse" title="Add to Favourites" data-toggle="tooltip"></i>
</span>
<input type="checkbox" name="favourites[]" value="2">
</label>
</li>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>