jQuery: Selecting elements that don't have a specific descendant element

旧巷老猫 提交于 2019-12-13 17:26:55

问题


How would I select all table elements that do not have any descendant td elements using jQuery 1.3.2?


回答1:


You could try:

$("table:not(:has(tbody > tr > td))").doStuff();

Working example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  $("table:not(:has(tbody > tr > td))").css("background", "yellow");
});
</script>
<style type="text/css">
table { border-collapse: collapse; }
td, th { border: 1px solid black; }
</style>
</head>
<body>
<table>
<tr>
  <td>First table</td>
</tr>
</table>
<table>
<tr>
  <th>Second table</th>
</tr>
</table>
</body>
</html>



回答2:


You're looking for the CSS :not() selector.

table *:not(td)

Should do it.

Edit: bah, misread what you wanted.



来源:https://stackoverflow.com/questions/1602098/jquery-selecting-elements-that-dont-have-a-specific-descendant-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!