使用jQuery,如何检查表格单元格是否为空?

Yas*_*nth 3 javascript jquery asp.net-ajax

使用jQuery,如何检查表格单元格是否为空?

请帮我.

jit*_*ter 10

空虚是什么意思.

//cell maycontain new lines,spaces,&npsp;,... but no real text or other element
$("cellselector").text().trim()=="";
Run Code Online (Sandbox Code Playgroud)

要么

//cell has no child elements at all not even text e.g. <td></td>
$("cellselector:empty")
Run Code Online (Sandbox Code Playgroud)


T.J*_*der 5

您可以使用带有该$函数的CSS选择器来获取对单元格元素的引用,然后使用该html函数来查看它是否包含任何内容.例如,如果单元格的ID为"foo":

if ($("#foo").html()) {
    // The cell has stuff in it
}
else {
    // The cell is empty
}
Run Code Online (Sandbox Code Playgroud)

  • 经过测试,适用于IE7,FF3.5,Chrome3,Safari4和Opera10.测试页:http://pastie.org/753004预期输出:"foo有内容/栏是空的"确保允许空格; 你可能想修剪`html`返回的字符串. (2认同)