【发布时间】: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