<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head style="height:100%">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/cupertino/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.js"></script> 
<script>
$(function() {
$("#tbl > tr").each(function(){
alert($(this).text());
});
});
</script></head>
<body style="margin:0px">
<table id="tbl" width="595" border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="red">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
</body>
</html>上面这段代码,没法通过$("#tbl > tr")这个子元素选择器查找到所有的tr,这是为什么呢?

解决方案 »

  1.   


    <script>
        $(function() {        
            $("#tbl tr").each(function(){
                alert($(this).text());            
            });                
        });
    </script>
    这样写就行了。
      

  2.   


     $("#tbl tr")这种查询器是后代查询器,不是子查询器。
    虽然在这里都适用,
    但是我的目的是想知道为什么子查询器在这里不好用。
      

  3.   

    我知道怎么写了:$("#tbl >tbody> tr")
      

  4.   


    嗯,table都有一个隐式的tbody。  原来jquery没有默认把tbody写在里面。呃~