jquery click tr会影响所有行,甚至是thead行

时间:2017-06-12 19:24:00

标签: jquery

以下是我的代码,工作正常。我可以点击表格中的一行,它可以完成我想要的操作。问题是它也会在<TH>上触发,我无法弄清楚如何使用.not('thead tr')之类的功能。我需要将TH转换为一种排序,因此我需要防止它在行单击时触发。

$('#upgrades').on("click", "#upgrades tr", function () {
*** do stuff
});

3 个答案:

答案 0 :(得分:4)

尝试":not(thead) tr" - .not()

这是你想要的吗?这样做无法点击tr

中的任何thead

$('#upgrades').on("click", ":not(thead) tr", function() { 
  console.log("clicked")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="upgrades">
  <thead>
    <tr>
      <td>cant click</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>can click</td>
    </tr>
  </tbody>
</table>

答案 1 :(得分:0)

使用.not排除不需要的表格行,下面的代码可以按照您的意愿执行:

$('tr').not('thead tr').click(function () {
*** do stuff
});
您的示例中的

建议:看起来您一直在使用id属性的公共值,因为id属性是预期的,所以使用类会更好在页面的整个html中每个元素都是唯一的。

祝你好运:)!

答案 2 :(得分:0)

你可以用这个

&#13;
&#13;
$('tr:not("thead tr")').on("click",function () {
      console.log("not thead");
 });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<table id="upgrades">
  <thead>
    <tr>
      <td>not clickable</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>clickable</td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;