如果用户点击<td>
,我希望这样的<tr>
没有有类型为复选框的输入元素,它将调用一个函数。
实际上,我正在尝试这样做:
$(document).on("click", "#mytable tr:has(td)", function (e) {
//Some code
}
我还尝试添加 tr:has(td):not(checkbox)
但也不起作用。
这样也行不通:
if (e.toElement === "input"){
return;
}
最佳答案
您使用了正确的选择器,: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>
关于jquery - 如果没有元素如何选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51769428/