gpt4 book ai didi

jQuery 无法选择表外的单元格

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:06 26 4
gpt4 key购买 nike

我知道使用 <td>外面<table>是一个无效标记,但它仍然是 HTML DOM 中的一个节点。这就是为什么我能够看到渲染的这两个单元格。 jQuery无法让我得到那些细胞。这是 JSBin Demo和代码

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

为什么 Num Cell2 为 0?我做错了什么?

最佳答案

那是浏览器在耍花招,jQuery 没有错。浏览器不会呈现 <tr>如果它不在 table 内并且只会剥离所有标签,只留下纯文本。在浏览器中检查您的元素面板以查看标记。没有类为 cell2 的元素

关于jQuery 无法选择表外的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com