【问题标题】:How to find the first row not with all cells filled by "&nbsp" with jQuery?如何使用jQuery找到第一行不是由“&nbsp”填充的所有单元格?
【发布时间】:2010-11-23 06:41:51
【问题描述】:
<table id="experiences" cellpadding="0" border="1" width="100%">
    <caption>table name</caption>
    <tr><th>col1</th><th>col2</th><th>col3</th><th>col4</th><th>col5</th><th>col6</th></tr>
    <tr><td>something</td><td>something</td><td>something</td><td>something</td><td>something</td><td>something</td></tr>
    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>

和上面一样,目标行应该是从上数第三行。

顺便说一句,我对jQuery不熟悉,$().eq(0)选择第一个,如何排除第一个?

$().ne(0) 会起作用吗?

【问题讨论】:

  • 原来是“ ”在 但不知何故消失了。
  •  用于追加空间。它不会出现在代码中。
  • 你用 :gt(0) 排除第一个
  • gt是great than的缩写,排除第二个怎么样?gt(1)会错过第一个
  • $("#experiences tr").gt(0).dblclick 好像不对,现在双击没有反应。

标签: javascript jquery jquery-selectors html-table


【解决方案1】:

应该这样做:

function not_just_nbsp() {
    return $(this).html() !== "#nbsp;";
}

$("#experiences td").filter(not_just_nbsp).filter(":first").parent();

只需将 nbsp 中的 # 替换为 & 符号即可。必须这样做才能在 StackOverflow 上显示。

【讨论】:

  • 我看不懂你的代码,它会选择第三行作为最终结果吗?
  • 这会在列中检测到超过 1 个空格吗?
  • 它找到所有表格单元格,过滤掉那些只有一个空格的单元格,选择剩余的最后一个单元格,然后找到父表格行。在您的示例中,它将选择第 4 行的第二个 tr。
  • @phoenix:如果您想排除所有只有空格的表格单元格(而不是那些只有一个   的表格单元格),您可以在过滤器函数中使用正则表达式。
  • 为什么使用 $(this).html() !== " ";而不是 $(this).html() !== " "?
【解决方案2】:

要选择大于第一行的所有行,您可以这样做:

$('#experiences td:gt(0)')

【讨论】:

  • $('.yourClass').gt(0) $('.yourClass:gt(0)') 一样吗?
  • 抱歉……来晚了。固定。
猜你喜欢
相关资源
最近更新 更多
热门标签