如果没有元素,如何 select

How to select if has not an element

我希望如果用户单击 <tr><td>,那么 没有 具有类型为 [=27 的输入元素=]checkbox,它会调用一个函数。

其实我是这么想的:

 $(document).on("click", "#mytable tr:has(td)", function (e) {
     //Some code
 }

我也试过添加 tr:has(td):not(checkbox) 但也没用。

而且这种方式也行不通:

if (e.toElement === "input"){
    return;
}

有办法吗?看看 table 怎么样:

您使用了正确的选择器,:not:has,但顺序不正确。试试这个:

$(document).on("click", "#mytable tr td:not(:has(:checkbox))", function(e) {
  console.log('this cell has no checkbox');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="mytable">
  <tr>
    <td><input type="checkbox" /></td>
    <td>Foo</td>
    <td>Bar</td>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>Fizz</td>
    <td>Buzz</td>
  </tr>
</table>

这是您需要的吗?请考虑使用此代码,tr 中的结构无关紧要,只要某处有一个复选框,它就可以工作

$(function(){
  $('td').on('click',function(){
  if( $(this).find('input[type="checkbox"]').length == 0 ){
     console.log('this td does not have a checkbox')
  }
  })
})
table{
width:100%;
}

td{
border:1px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="radio"/>
    </td>
    <td>
      <input type="checkbox"/>
    </td>
    <td>
      <p>
         <span>
            <input type="checkbox"/>
         </span>
      </p>
    </td>
  </tr>
</table>