Jquery 在$.each()中洗牌多个数组
我以以下方式在段落内生成超链接:Jquery 在$.each()中洗牌多个数组,jquery,Jquery,我以以下方式在段落内生成超链接: <p id="category1"></p> jQuery代码: $(function () { var link = [ ["category1", "http://www.xyzabcxyz.com/apple", "Apple description comes here"], ["category2", "http://www.xyzabcxyz.com/banana", "Banan
<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>');
}
});
});
$(函数(){
变量链接=[
[“类别1”http://www.xyzabcxyz.com/apple“,“苹果的描述来了”],
[“类别2”http://www.xyzabcxyz.com/banana“,“香蕉说明来了”],
[“类别3”http://www.xyzabcxyz.com/apricots“,“杏子的描述来了”],
[“类别4”http://www.xyzabcxyz.com/peaches“,“桃子描述来了”]
];
$。每个(链接、功能(e){
如果($(“#”+链接[e][0])[0]){
$(“#”+链接[e][0])。附加(“”);
}
});
});
演示:
到目前为止还不错。一切正常
我想知道如何更改我的代码,使其在一个类别中随机地洗牌多个产品。大概是这样的:
$(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>');
}
});
});
$(函数(){
变量链接=[
[“类别1”http://www.xyzabcxyz.com/apple“,“苹果的描述来了”],
"http://www.xyzabcxyz.com/pineapple“,“菠萝描述来了”],
"http://www.xyzabcxyz.com/lemon“,“Lemon description来了”]],
[“类别2”http://www.xyzabcxyz.com/banana“,“香蕉说明来了”],
[“类别3”http://www.xyzabcxyz.com/apricots“,“杏子的描述来了”],
"http://www.xyzabcxyz.com/Berries“,“浆果说明到此”]]
[“类别4”http://www.xyzabcxyz.com/peaches“,“桃子描述来了”]
];
$。每个(链接、功能(e){
如果($(“#”+链接[e][0])[0]){
$(“#”+链接[e][0])。附加(“”);
}
});
});
因此,对于一个用户,它可能会显示一个关于苹果的超链接,而对于另一个用户,它可能是一个关于柠檬的超链接。如果同一访问者刷新页面,则应显示同一目录的新产品
附言:我想要一个随机链接,但如果访问者a看到了这个链接,我就不需要使用cookie来跟踪。同一用户在刷新时可能会看到同一产品两次,这很正常。您可以使用math.random()从类别数组中获取随机链接,并使用cookie或localStorage跟踪已看到的链接。尝试以下操作:
<!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>
函数getRandomInt(最小值、最大值){
如果(max==0)返回0;
返回Math.floor(Math.random()*(max-min+1))+min;
}
$(函数(){
变量链接=[
[“类别1”,[”http://www.xyzabcxyz.com/apple“,“苹果的描述来了”],
["http://www.xyzabcxyz.com/pineapple“,“菠萝描述来了”],
["http://www.xyzabcxyz.com/lemon“,“Lemon description来了”]],
[“类别2”,[”http://www.xyzabcxyz.com/banana“,”香蕉说明在此“]]],
[“类别3”,[”http://www.xyzabcxyz.com/apricots“,“杏子的描述来了”],
["http://www.xyzabcxyz.com/Berries“,”浆果描述在此“]]],
[“类别4”,[”http://www.xyzabcxyz.com/peaches“,“桃子的描述来了”]]
];
$。每个(链接、功能(e、v){
if($(“#”+v[0])长度){
var min=0;
var max=(v[1]。长度)-1;
var i=getRandomInt(最小值、最大值);
$(“#”+v[0])。附加(“”);
}
});
});
对于随机函数tks。到我想要一个随机链接,但如果访问者a看到该链接,我不需要使用cookie来跟踪。同一个用户在刷新时可能会看到同一个产品两次,这完全没关系。好吧,那就这样吧。随机性很简单。