HTML结构如下<tr>
    <td><a class="gotop" href="javascript:void(0)">顶1</a></td>
</tr>
<tr>
    <td><a class="gotop" href="javascript:void(0)">顶2</a></td>
</tr>
<tr>
    <td><a class="gotop" href="javascript:void(0)">顶3</a></td>
</tr>
<tr>
    <td><a class="gotop" href="javascript:void(0)">顶4</a></td>
</tr>
gotop绑定事件,找到前一个gotop:方法一:
$('.gotop').click(function(){
var prev=$(this).parents('tr').prev().find('.gotop')
})
方法二:
$('.gotop').click(function(){
var i=$('.gotop').index($(this)
var pi=i-1
pi=(pi>=0)?pi:0
var prev=$('.gotop:eq('+pi+')')
})方法三 ,此方法错误。
$('.gotop').click(function(){
var prev=$(this).prev('.gotop')
})求教有没更简单的方式可以找到上一个gotop?
试过prev,prevAll,prevUnitl,但是似乎prev开头的只能找同一个等级的,即,一个标签下的,请指教!

解决方案 »

  1.   

    class绑到tr上不就简单了么??
      

  2.   

    下面也可能会这样
    <tr>
        <td><a class="gotop" href="javascript:void(0)">顶1</a></td>
    <td><a class="godown" href="javascript:void(0)">沉1</a></td></tr>
    <tr>
        <td><a class="gotop" href="javascript:void(0)">顶2</a></td>
    <td><a class="godown" href="javascript:void(0)">沉2</a></td></tr>
      

  3.   


            var $gt = $('.gotop');
            $gt.click(function(){
                    var index = $gt.index(this)
                    index && alert("上一个是:"+$($gt[index-1]).text() )
            })
      

  4.   

    感谢虎哥。
    另有一不明白。index && alert("上一个是:"+$($gt[index-1]).text() )这个 && “与” 作用 后,如果是第一个 为什么就不alert了?alert 难道可以看成一个true or false么?觉得这个挺有意思的,请指教!
      

  5.   

    :-),你一猜就对。不过是一个小把戏,相当与
    if(index) alert("上一个是:"+$($gt[index-1]).text() )
      

  6.   

    再次感谢虎哥。原来 var xx=0 的时候 if (xx) 始终为false。