jQuery无法选择表外的单元格 [英] jQuery is unable to select cells outside table
问题描述
我知道在< table>
之外使用< td>
是一个无效的标记,但它仍然是HTML DOM中的节点。这就是为什么我能够看到这两个单元格呈现的原因。 jQuery
无法获取这些单元格。这是 JSBin演示和代码
I know using <td>
outside <table>
is an invalid markup but it is still a node in HTML DOM. That is why I am able to see those two cells rendered. jQuery
is unable to get me those cells. Here is the JSBin Demo and Code
<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
JS
var $a = $('#a');
var numCells1 = $('.cell1').length;
var numCells2 = $('.cell2').length;
$a.html('Num Cell1:' + numCells1 + ' Num Cell2:' + numCells2);
输出
Output
Num Cell1:1 Num Cell2:0
为什么Num Cell2为0?我做错了什么?
Why Num Cell2 is 0? What am I doing wrong ?
推荐答案
这是浏览器正在做的伎俩,jQuery没有错。如果浏览器不在表
内,则浏览器不会呈现< tr>
,并且只会删除所有标签只留下纯文本。检查浏览器中的元素面板以查看标记..没有类别 cell2的元素
That is the browser doing its trick, nothing wrong with jQuery. The browser won't render the <tr>
if its not within a table
and will just strip off all the tags leaving out just plain text. Check your elements panel in browser to see the markup.. there is no element with a class of cell2
这篇关于jQuery无法选择表外的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!