gpt4 book ai didi

jquery - 在 $.each() 中打乱多个数组

转载 作者:行者123 更新时间:2023-12-01 01:43:33 24 4
gpt4 key购买 nike

我通过以下方式在段落内生成超链接:

<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:我想要一个随机链接,但不希望达到必须使用 cookie 来跟踪访问者 A 是否看到该链接的程度。同一用户可能会在刷新时两次看到相同的产品,这完全没问题。

最佳答案

试试这个:

<!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

关于jquery - 在 $.each() 中打乱多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32586402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com