简体   繁体   English

在jquery中可以使表行但不是表标题可单击

[英]Making table rows but not table headers clickable in jquery

Currently I am using 目前我正在使用

$('#mytable tr').click(function() {
    blah blah
});

This makes all rows, including the headers clickable. 这使得所有行,包括标题可点击。 How can I exclude the headers or <th> 's? 如何排除标题或<th>

Separate you header and body using <thead> and <tbody> tags, and change your selector to "#mytable tbody tr" 使用<thead><tbody>标签分隔标题和正文,并将选择器更改为"#mytable tbody tr"

HTML will look something like this HTML将看起来像这样

<table> 
    <thead>
        <tr>
         ...
        </tr>
    </thead>
    <tbody>
         <tr>
            ...
         </tr>
    </tbody>
</table>

The easiest way, assuming you've marked your table up accurately, is to use: 假设您准确标记了table ,最简单的方法是使用:

 $('#mytable tbody tr').click(function() {
     blah blah
 });

Failing that: 失败了:

 $('#mytable tr').filter(
     function(){
         return $(this).find('td').length;
     }).click(function() {
     $(this).addClass('clicked');
 });

JS Fiddle demo . JS小提琴演示

You can remove them with the not function: 您可以使用not函数删除它们:

$('#mytable tr').not('th').click(function() {
   blah blah
});

Or: 要么:

$('#mytable tr:not(th)').click(function() {
   blah blah
});

Use Jquery delegate 使用Jquery委托

$("#mytable").delegate("td", "click", function() {
  //Do something
});

this should work 这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM