查找直系亲属,不再使用jQuery
Find immediate children and go no further using jQuery
在搜索特定元素时,如何获取直属子项?例如,我想要获得表t1
的tr
元素。
<table id="t1" bgcolor="yellow">
<tbody>
<tr>
<td>This is Cell 1</td>
<td>This is Cell 2</td>
</tr>
<tr>
<td>This is Cell 3</td>
<td>
<table id="t2" bgcolor="red">
<tbody>
<tr>
<td>This is Cell 1</td>
<td>This is Cell 2</td>
</tr>
<tr>
<td>This is Cell 3</td>
<td>This is Cell 4</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
我试过这个:
'Count = ' + $('#t1 tbody').children('tr').length;
然而,我得到了4的计数,我不明白为什么?
下面是一个完整的示例:
使用:
'Count = ' + $('#t1 > tbody').children('tr').length;
// or: $("#t1 > tbody > tr").length
// or: $("#t1")[0].rows.length; // In this case, equal to previous code.
// Warning: This also includes the rows from
// the <thead> and <tfoot> sections.
您当前的代码显示4,因为表#t1
:中有两个<tbody>
元素
<table id="t1" bgcolor="yellow"> <-- #t1
<tbody> <--- tbody
<tr> ... </tr> <----- Child 1
<tr> ... <----- Child 2
<tbody> <--- tbody (unexpected?)
<tr> ... </tr> <----- Child 3
<tr> ... </tr> <----- Child 4
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
这是因为使用$('#t1 tbody')
可以从两个表中获得tbody
您可以直接使用Child Selector (“parent > child”)
docs
$('#t1 > tbody > tr').length;
这是您更新的示例:http://jsfiddle.net/SvygZ/1/
相关文章:
- 不再需要时使用jQuery/kill实例
- jquery ui模式框中的表单隐藏后,键盘将不再被调用
- 为什么不是'我的jQuery不再工作了
- jQuery - DIV 关闭整个网站后不再出现
- 不再使用 jQuery 悬停时隐藏
- Jquery 点击事件不再绑定新创建的标签
- .click() 不再工作 jquery.
- 当项目不再聚焦时,jQuery 停止脚本
- Ajax调用(使用jquery)在5分钟后不再得到响应
- 查找直系亲属,不再使用jQuery
- jQuery Mobile中的全局变量不再工作
- Don'不再显示此页面(JQUERY cookie)
- Jquery点击事件不'不再显示按钮
- jQuery json Ajax调用不再工作后第一次(包括表单提交,setTimeout)
- jQuery -当你委托一个事件处理程序时,它是否也会自动从不再匹配的元素中移除自己?
- jQuery从1.5.X到1.7.x使XML不再工作
- jquery .load()后,Javascript灯箱效果不再工作
- jquery抛!组件:highchart不再是交互式的
- JQuery POST数据到php,直接到php,数据不再存在于php
- JQuery库不再触发的问题