我对具有特定类的元素使用click事件.在click事件函数中,我需要使用相同的类获取页面上的下一个元素.我试过了:
$('.class').click(function() { var $next = $(this).next('.class') });
和:
$('.class').click(function() { var $next = $(this).parent().parent().parent().next('.class') });
.class是表中td内的div,这就是为什么三次parent().
我的HTML是这样的(.class是.drag):
<table cellspacing="0" cellpadding="0" border="0" class="black8"> <tr class="trow1 drop trow1_over" rel="0,1"> <td> <div class="drop rootFolder" rel="0,1"></div> </td> <td width="100%" class="folderListOnclick"> <span>.. (koreňový adresár)</span> </td> <td> </td> <td> </td> </tr><tr class="trow1"> <td> <div class="drag drop ordinaryFolder" rel="1,1" style="width: 40px;"> </div> </td> <td width="100%" class="folderListOnclick" rel="1"> <span>aaa</span> </td> <td> <div class="button_mini folderEditOnclick" rel="1"> <span></span> </div> </td> <td> <div class="button_mini folderDeleteOnclick" rel="1"> <span><span> </div> </td> </tr><tr class="trow1"> <td> <div class="drag drop ordinaryFolder" rel="19,1" style="width: 50px;"> </div> </td> <td width="100%" class="folderListOnclick" rel="19"> <span>subaaa</span> </td> <td> <div class="button_mini folderEditOnclick" rel="19"> <span></span> </div> </td> <td> <div class="button_mini folderDeleteOnclick" rel="19"> <span><span> </div> </td> </tr> </table>
解决方法
你可以试试:
$(".drag").click(function() { var draggables = $(this).parents("table").find(".drag"); var $next = draggables.filter(":gt(" + draggables.index(this) + ")").first(); });