2016-06-09 239 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