【问题标题】:Shuffling multiple arrays in $.each()在 $.each() 中洗牌多个数组
【发布时间】:2015-12-11 17:30:58
【问题描述】:

我正在通过以下方式在 para 中生成超链接:

<p id="category1"></p>

jQuery 代码:

$(function () {
    var link = [
        ["category1", "http://www.xyzabcxyz.com/apple", "Apple description comes here"],
        ["category2", "http://www.xyzabcxyz.com/banana", "Banana description comes here"],
        ["category3", "http://www.xyzabcxyz.com/apricots", "Apricots description comes here"],
        ["category4", "http://www.xyzabcxyz.com/peaches", "Peaches description comes here"]
    ];
    $.each(link, function (e) {
       if ($("#" + link[e][0])[0]) {
           $("#" + link[e][0]).append('<a target="_blank" href="' + link[e][1] +
                       '">' + link[e][2] + '</a>');
       }
    });
});

演示:http://jsfiddle.net/j2g411yk/

到目前为止一切顺利。一切正常。

我想知道如何更改我的代码,以便它随机打乱一个类别中的多个产品。像这样的:

$(function () {
    var link = [
        [["category1", "http://www.xyzabcxyz.com/apple", "Apple description comes here"],
                       "http://www.xyzabcxyz.com/pineapple", "Pineapple description comes here"],
                       "http://www.xyzabcxyz.com/lemon", "Lemon description comes here"]],
        ["category2",  "http://www.xyzabcxyz.com/banana", "Banana description comes here"],
        [["category3", "http://www.xyzabcxyz.com/apricots", "Apricots description comes here"],
                       "http://www.xyzabcxyz.com/Berries", "Berries description comes here"]]
        ["category4", "http://www.xyzabcxyz.com/peaches", "Peaches description comes here"]
    ];
    $.each(link, function (e) {
       if ($("#" + link[e][0])[0]) {
           $("#" + link[e][0]).append('<a target="_blank" href="' + link[e][1] +
                       '">' + link[e][2] + '</a>');
       }
    });
});

因此,对于一个用户,它可能会显示一个关于 Apple 的超链接,而对于另一个用户,它可能是一个关于 Lemon 的超链接。如果同一访问者刷新页面,则应显示同一类别的新产品。

P.S:我想要一个随机链接,但如果访客 A 看到该链接,我需要使用 cookie 来跟踪。同一用户可能会在刷新时两次看到同一产品,这完全没问题。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您可以使用 math.random() 从类别数组中获取随机链接,并使用 cookie 或 localStorage 来跟踪已看到哪些链接。

    【讨论】:

    • 我想要一个随机链接,但如果访客 A 看到该链接,我必须使用 cookie 来跟踪。同一用户可能会在刷新时两次看到相同的产品,这完全没问题。
    • 那么你就去吧。随机很简单。
    【解决方案2】:

    试试这个:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <script type='text/javascript' src='js/jquery.min.js'></script>
    </head>
    <body>
        <p id="category1"></p>
        <p id="category4"></p>
        <script>
            function getRandomInt (min, max) {
                if (max == 0) return 0;
                return Math.floor(Math.random() * (max - min + 1)) + min;
            } 
    
            $(function () {
                var link = [
                    ["category1", [[ "http://www.xyzabcxyz.com/apple", "Apple description comes here"],
                                   ["http://www.xyzabcxyz.com/pineapple", "Pineapple description comes here"],
                                   ["http://www.xyzabcxyz.com/lemon", "Lemon description comes here"]]],
                    ["category2", [["http://www.xyzabcxyz.com/banana", "Banana description comes here"]]],
                    ["category3", [["http://www.xyzabcxyz.com/apricots", "Apricots description comes here"],
                                   ["http://www.xyzabcxyz.com/Berries", "Berries description comes here"]]],
                    ["category4", [["http://www.xyzabcxyz.com/peaches", "Peaches description comes here"]]]
                ];
                $.each(link, function (e,v) {
                    if ($("#" + v[0]).length) {
                        var min = 0; 
                        var max = (v[1].length)-1;
                        var i = getRandomInt(min,max);
    
                        $("#" + v[0]).append('<a target="_blank" href="' + v[1][i][0] +'">' + v[1][i][1] + '</a>');
                    }
                });
            });
        </script>   
    </body>
    

    对于随机函数 tks。致
    Javascript: Generate a random number within a range using crypto.getRandomValues

    【讨论】:

      猜你喜欢
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多