如何在jQuery中获取下一个或上一个属性id?

Ste*_*ffi 7 javascript ajax jquery attributes jquery-attributes

我有以下代码

<a class="getty" id="1" href="/...">One<./a>
<a class="getty" id="2" href="/...">Two<./a> 
<a class="getty" id="3" href="/...">Three<./a>
Run Code Online (Sandbox Code Playgroud)

当我点击左或右时,我需要获取之前的ID.
示例:如果我在id="2",我需要id="1"点击左边.

$(document).keydown(function(e){
    if (e.keyCode == 37) {
       $('.getty').attr("id");
       return false;
    } });
    if (e.keyCode == 33) {
       $('.getty').attr("id");
       return false;
    } 
});
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点 ?

谢谢

Hus*_*ein 10

单击链接时获取先前的ID.

$('.getty').click(function(e) {
    e.preventDefault();
    var x = $(this).prev().attr('id');
    alert(x);

});
Run Code Online (Sandbox Code Playgroud)

您可以使用该next()功能为下一步做同样的事情

查看http://jsfiddle.net/H3hdy/上的工作示例