2012-10-22 34 views
-1
<table> 
<tr class="csstablelisttd"> 
    <td> 
    </td> 
    <td> 
     15 
    </td> 
</tr> 
<tr class="csstablelisttd"> 
    <td> 
    </td> 
    <td> 
     30 
    </td> 
</tr> 
<tr class="csstablelisttd"> 
    <td> 
    </td> 
    <td> 
     45 
    </td> 
</tr> 

我如何獲得鼠標山坳指數級的下降jQuery的

$(".csstablelisttd").live('mousedown', function(e) { 
    lastRow = $(this).closest("tr")[0].rowIndex; 
} 

我怎樣才能在鼠標列索引中的jQuery類的下來嗎?

+2

目前還不清楚是否要列或行索引 –

+0

我想列索引 – lax

+0

你可以試試jQuery的索引(); http://api.jquery.com/index/ –

回答

2

要得到列的索引,您可以使用index功能:

$(".csstablelisttd td").on('mousedown', function (e) { 
    var colIndex = $(this).index(); 
}); 

DEMONSTRATION

請注意,您必須對td,而不是tr檢測到事件。

+0

我不想更改html – lax

+3

這不會更改HTML。 –

+0

我必須得到鼠標只有css類的顏色索引沒有額外的td – lax

1

列索引是以下幾點:

$('.csstablelisttd').mousedown(function (e) { 
    var colIndex = $(e.target).closest('td').index(); 
}); 

我已經更新您的fiddle

+0

Thaks我有一個問題,我只能在mousedown上的colIndex上面添加class上面的類 – lax

+0

$(this).children(「:not(:first)」)。addClass(「csstdhighlight」); – lax

+0

http://jsfiddle.net/jquYB/ – lax

0

您正在監聽行本身的事件,該事件根據定義跨越表中的所有可用列。你可以做這樣的事情:

$('tr.csstablelisttd td').on('mouseover', function() 
{ 
    var col = $(this).parent().children().index($(this)), 
     row = $(this).parent().parent().children().index($(this).parent()); 

    alert('Row: ' + row + ', Column: ' + col); 
}); 
+0

如何在當前colIndex上添加類 – lax

1

您當前的代碼檢查trindex,選擇td代替:

$(".csstablelisttd td").live('mousedown', function(e) { 
    idx = $(this).index(); 
}); 

Example fiddle