如何设置 Javascript 中随机生成的项目列表的样式

How to style a randomly generated list of items in Javascript

使用Underscorejs的示例方法。

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

Code: (from UnderscoreJS)

sample_.sample(list, [n]) Produce a random sample from the list. Pass a number to return n random elements from the list. Otherwise a single random item will be returned.

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

我希望你正在寻找一种方法来设置它们的样式randomly.Please不要认为这是最好的解决方案;您可以创建一个变量来存储随机 类,并将它们添加到 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, You can view the fiddle here 上创建了一个 fiddle。如果您发现麻烦,请告诉我

编辑:

HERE you GO。 SORRY 第一次没看懂问题