2015-01-10 31 views
0

如何找到哪個孩子有'正常排序表'類使用jQuery的表類'nosort'?我想用'nosort'類來提醒列號。如何找到哪個孩子有使用jQuery的表的特定類?

<table class="normal-sort-table"> 
    <thead> 
     <tr> 
      <th>#</th> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th class="nosort">Username</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>1</td> 
      <td>Mark</td> 
      <td>Otto</td> 
      <td>mark246</td> 
     </tr> 
    </tbody> 
</table> 
<script> 
$(document).ready(function() { 

    if ($('.normal-sort-table tr th').hasClass("nosort")) {alert('column-number-with-nosort-class-alert-here')} 


}) 
</script> 

回答

1

可以使用JQ .index()方法:

$('.normal-sort-table tr th.nosort').index() 
1
alert($("th").index($(".nosort"))); 
+0

是否也能解釋你的答案? – Magnilex

+0

它在Jquery文檔中進行了解釋:http://api.jquery.com/index/。 – yeswanth

+0

當然,但如果您將相關部分添加到您的問題中,這也很有幫助。 – Magnilex

0

如果你想確切的列數,那麼你必須寫

alert($('th.nosort').index()+1); 

監守從零開始的索引。

alert($('th.nosort').index()); 

alert($('th.nosort').index()+1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<table class="normal-sort-table"> 
 
     <thead> 
 
      <tr> 
 
       <th>#</th> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th class="nosort">Username</th> 
 
      </tr> 
 
     </thead> 
 
     <tbody> 
 
      <tr> 
 
       <td>1</td> 
 
       <td>Mark</td> 
 
       <td>Otto</td> 
 
       <td>mark246</td> 
 
      </tr> 
 
     </tbody> 
 
    </table>

相關問題