我知道使用<td>
以外<table>
是无效的标记,但它仍然是HTML DOM中的一个节点。这就是为什么我能够看到这两个单元格呈现。 jQuery
无法让我看到这些单元格。这里是JSBin Demo和代码jQuery无法选择表格外部的单元格
HTML
<table>
<tr><td class='cell1'>Cell inside table</td></tr>
</table>
<tr>
<td class='cell2'>Cells without table</td>
<td class='cell2'>Cells without table</td>
</tr>
JS
var $a = $('#a');
var numCells1 = $('.cell1').length;
var numCells2 = $('.cell2').length;
$a.html('Num Cell1:' + numCells1 + ' Num Cell2:' + numCells2);
输出
Num Cell1:1 Num Cell2:0
为什么民小区2为0?我究竟做错了什么 ?
你的第二个查询更改为'$(“TD”) .length'结果为'1' ...所以我想无效的标记被忽略。有趣。 –
请注意,您可以在jQuery中创建partials(不在文档中的DOM元素层次结构),并稍后将它们插入到文档中。如下所述,您不能将不正确的元素放入文档本身。 –
@JimCote我明白,但我的疑问是为什么浏览器删除''标签,但不是''标签在这种情况下http://jsbin.com/wimixxi/2/edit?html,css,js,output –
sachinjain024