2016-06-09 244 views
3

我得到了这段代码,一切都好,功能齐全。 当我点击时,我需要改变心脏颜色的选项。Boostrap改变颜色点击

现在:

点击灰色的空心脏将充分灰色充分的心脏

我需要: 点击灰色的空心脏将全力冲满心脏

$(function($) { 
 
    $(document).on('click', '.box-btn', function(event) { 
 
    var target = $(event.target).closest('.wrapper'); 
 
    target.find('.glyphicon').toggleClass('glyphicon-heart-empty glyphicon-heart'); 
 
\t 
 
    
 
    }); 
 
});
.box-btn { 
 
    float:left; 
 
    background: transparent; 
 
    font-size: 53px; 
 
    color: #4D4E56; 
 
    font-weight: 300; 
 
    cursor: pointer; 
 
    border-radius: 2px; 
 
    line-height: 0.5; 
 
    border: none; 
 
\t outline:none; 
 

 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button type="submit" class="box-btn"><span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span></button>

回答

4

您可以使用另一个类来使用用Jquery更改颜色。

试试这个:

$(function($) { 
 
    $(document).on('click', '.box-btn', function(event) { 
 
    $(this).find('.glyphicon').toggleClass('red').toggleClass('glyphicon-heart-empty glyphicon-heart'); 
 
    }); 
 
});
.box-btn { 
 
    float: left; 
 
    background: transparent; 
 
    font-size: 53px; 
 
    color: #4D4E56; 
 
    font-weight: 300; 
 
    cursor: pointer; 
 
    border-radius: 2px; 
 
    line-height: 0.5; 
 
    border: none; 
 
    outline: none; 
 
} 
 
.glyphicon { 
 
    transition: color .3s linear; 
 
} 
 
.box-btn .red { 
 
    color: red; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button type="submit" class="box-btn"><span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span> 
 
</button>

+0

出于某种原因,如果我使用(这一点),而不是目标,没有什么happend,如果我使用的目标,并添加以下的toogle红,则心脏是灰色的再次,你有什么想法可以造成这种情况吗? –

+0

@MR.Don'tknow我改变为* this *以使您的工作成为您提供的标记的例子。通过该目标您正在搜索不存在的元素包装器。如果你可以用更多的代码做一个小提琴,我可以帮你 – DaniP