i n*_*elp 2 size jquery children
我刚发现孩子的体型不一致.
下面附上带警报的完整代码,以便于参考.
我得到数据错误的方式是什么?
<body>
<table width="100" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30" valign="top"><strong>Header Title</strong></td>
</tr>
<tr>
<td height="32" valign="top">Date : <strong>01/01/2010 </strong> <br><div><b></b></div><span></span></td>
</tr>
</table>
</body>
Run Code Online (Sandbox Code Playgroud)
$("td").each(function() {
alert($(this).children().size());
});
//first td showing 1 direct children- <strong>
//second td showing 4 direct children- <strong> <br> <div> <span>
-----
$("tr").each(function() {
alert($(this).children().size());
});
//first tr showing 1 direct children - <td>
//second tr showing 1 direct children - <td>
-----
$("table").each(function() {
alert($(this).children().size());
});
// ERROR
// this table showing 1 direct children only.... something WRONG.
// I thought there are 2 <tr> inside this table?
Run Code Online (Sandbox Code Playgroud)
原因是<tbody>你的桌子里没有.浏览器会自动为您添加,因此它成为唯一的孩子<table>.
您可能有兴趣运行这段代码:
alert($('table').children()[0].tagName);
Run Code Online (Sandbox Code Playgroud)