简单地说,我有一个带有data-next
, data-prev
, data-cat
attr 的 div。
这是到目前为止的js..
$(document).keydown(function(event) {
var prev = $("#myDiv").attr('data-prev'),
cat = $("#myDiv").attr('data-cat'),
next = $("#myDiv").attr('data-next');
switch (event.keyCode) {
case 37:
if (prev >= 1) {
window.location = "http://site.com/picture/" + prev;
}
break;
case 39:
if (next > 1) {
window.location = "http://site.com/picture/" + next;
}
break;
}
});
该代码有效,但仅适用于左右动作。对于猫,我认为这样做,但我认为是错误的,有更好的方法来做到这一点!
$(document).keydown(function(event) {
var prev = $("#myDiv").attr('data-prev'),
cat = $("#myDiv").attr('data-cat'),
next = $("#myDiv").attr('data-next');
if (cat) {
switch (event.keyCode) {
case 37:
if (prev >= 1) {
window.location = "http://site.com/picture/" + prev;
}
break;
case 39:
if (next > 1) {
window.location = "http://site.com/picture/" + next;
}
break;
}
}
else {
switch (event.keyCode) {
// but i dont how to insert the cat var the link have to be cat/{n}/picture/{n}
case 37:
if (prev >= 1) {
window.location = "http://site.com/picture/" + prev;
}
break;
case 39:
if (next > 1) {
window.location = "http://site.com/picture/" + next;
}
break;
}
}
});