2016-07-29 36 views
0

在流星中,我试图改变点击图标变化(我使用glyphicon图标)添加收藏夹列表。我使用了toggleClass()。但它不起作用。我在这里附上我的代码。当我刷新页面时,图标不会改变。任何人都可以帮助我解决方案。 HTML代码:如何更改点击流星的图标

<span class="glyphicon glyphicon-star-empty" style="color:green"></span> 

和JS代码:

$(document).ready(function(){ 

    $('.glyphicon').click(function(){ 
    $(this).toggleClass('glyphicon-star-empty glyphicon-star'); 
    }); 
}); 
+0

它工作正常..你添加jquery.js?检查这个链接:https://jsfiddle.net/w42mr1ju/ –

+0

1)检查min.js文件。包括与否。 –

回答

0

你必须定义一个事件:

Template.YourTemplateName.events({ 
    "click .glyphicon": function(){ 
    $(event.target).toggleClass('glyphicon-star-empty glyphicon-star'); 
    } 
}); 
+0

'this'将是模板的数据上下文,而不是DOM元素。你需要'$(event.target).toggleClass()' –

+0

Right @MichelFloyd –

0

这应该工作:
Template.TemplateName.events({ $('.glyphicon').click(function(){ $(event.currentTarget).toggleClass('glyphicon-star-empty glyphicon-star'); });

2

这是怎么了你这样做:

Template.TemplateName.events({ 
    "click .glyphicon": function(event){ 
     $(event.currentTarget).toggleClass('glyphicon-star-empty glyphicon-star'); 
});