如何在Javascript

时间:2016-04-18 23:55:33

标签: javascript css underscore.js

使用Underscorejs的示例方法。

是否可以将css样式添加到生成的随机样本中?例如,在返回的[1,6,2]数组中,如何将所有3个返回的数字设为不同的颜色?

  

代码:(来自UnderscoreJS)

     

sample_.sample(list,[n])从列表中生成随机样本。通过   一个数字,用于从列表中返回n个随机元素。否则单一   随机项目将被退回。

_.sample([1, 2, 3, 4, 5, 6], 3);
=> [1, 6, 2]

1 个答案:

答案 0 :(得分:3)

我希望您正在寻找一种随意设计风格的方法。请不要认为这是最佳解决方案;你可以创建一个变量来存储随机类,并将它们添加到quote元素中;我使用以下代码

实现了这一点
    var my_res = _.sample([
        'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et',
        'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab',
        'Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot',
        'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated',
        'A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with',
        'One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.'
    ], 3);
    var my_class = _.sample(['style_1','style_2','style_3','style_4','style_5','style_6'],3)
    var arr_len = my_res.length;
    var targ = document.getElementById('i_need_quotes_within_this');
    for(i=0; i<arr_len; i++){
        targ.innerHTML += "<q class="+my_class[i]+">"+my_res[i]+"</q><br/>"
    }

我还在jsfiddle创建了一个小提琴,您可以查看小提琴here。如果您发现麻烦,请告诉我

编辑:

HERE you GO。抱歉,我第一次没有理解这个问题