流星随机颜色选择器

时间:2016-11-15 09:18:44

标签: javascript jquery html meteor

我写了这个函数,它应该从这个数组中显示一个随机颜色,但它总是显示第一个颜色。我的代码有问题吗?我试过谷歌搜索并查看了流星文档,但没有发现任何有用的东西。

randomInitials: function () {
   var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   var nLetter = chars.charAt(Math.floor(Math.random()*chars.length));
   var sLetter = chars.charAt(Math.floor(Math.random()*chars.length));
   var colors = ["#e57373","#f06292","#ba68c8","#9575cd","#7986cb","#64b5f6","#4fc3f7","#4dd0e1","#4db6ac","#81c784","#aed581","#dce775","#fff176","#ffd54f","#ffb74d","#ff8a65","#a1887f","#e0e0e0","#90a4ae"];

   return colors[Math.floor(Math.random()*colors.length)];
   return nLetter + sLetter;
},
<div class="other-profile">
    <span>{{memberData.profile.initials}}</span>
</div>

现在它看起来像这样: Now

我想要这个:

This

1 个答案:

答案 0 :(得分:2)

由于您只返回颜色,因此您的助手需要看起来像

<div class="other-profile" style="background-color: {{randomInitials}}">
    <span>{{memberData.profile.initials}}</span>
</div>

在你的模板中:

        // ....
        while(x<in.length())
        {
            // This is actually checking the number stored in x
            // a number is never a letter
            b1 = Character.isLetter(x);
            // ....         
        }