jQuery 测试是否第一个孩子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5012639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
test if first child
提问by frenchie
I'm testing to see if a row in a table is the first child. So far I have:
我正在测试表中的一行是否是第一个孩子。到目前为止,我有:
//the loop works
$('#GridFolderPopup').find('tr').each(function () {
if ( $(this).is(:first) == true ) //this is the faulty line
{
$(this).addClass('GridFolderRow');
};
});
Any suggestions?
有什么建议?
Thanks
谢谢
采纳答案by Mash
This may be a solution:
这可能是一个解决方案:
$('#GridFolderPopup tr:first').addClass('GridFolderRow');
Also reduce the code a lot.
也减少了很多代码。
回答by Nikita Rybak
Try .is(':first')
instead of .is(:first)
.
尝试.is(':first')
代替.is(:first)
.
You might be confusing Javascript with Ruby: in Javascript, colon can't be used like this (outside of string).
您可能会将 Javascript 与 Ruby 混淆:在 Javascript 中,不能像这样使用冒号(字符串之外)。
edit
Also, in your case ':first-child'
selector might be more appropriate. See the docs about the difference
编辑
此外,在您的情况下,':first-child'
选择器可能更合适。请参阅有关差异的文档
http://api.jquery.com/first-child-selector/
http://api.jquery.com/first-selector/
http://api.jquery.com/first-child-selector/
http://api.jquery.com/first-selector/
回答by marcv
Simply asking for the first child of the table
/tbody
is muchmore efficient than testing every single row in it. The larger the table, the bigger the difference.
简单地问了的第一个孩子table
/tbody
是多少比它测试每一行更有效。表越大,差异越大。
Therefore :
所以 :
if you want to "see if a[given] row in a table is the first child":
if ($(a_given_row).prev().length === 0) { // no element before this row, so it's the first one. }
if you want to get the first row of a table:
// Directly ask the DOM for the first element child of the table/tbody var firstRow = a_given_table.firstChild; do { firstRow = firstRow.nextSibling; } while (firstRow.nodeType !== 1) // If you don't need to support IE8-, you can use this single line instead: var firstRow = a_given_table.firstChildElement;
See performance comparison here: http://jsperf.com/get-first-child-element
如果您想“查看表中的[给定]行是否是第一个孩子”:
if ($(a_given_row).prev().length === 0) { // no element before this row, so it's the first one. }
如果您想获取表格的第一行:
// Directly ask the DOM for the first element child of the table/tbody var firstRow = a_given_table.firstChild; do { firstRow = firstRow.nextSibling; } while (firstRow.nodeType !== 1) // If you don't need to support IE8-, you can use this single line instead: var firstRow = a_given_table.firstChildElement;
在此处查看性能比较:http: //jsperf.com/get-first-child-element
回答by Sonal Patil
$('#GridFolderPopup').find('tr').eq(0).addClass('GridFolderRow');
回答by Sonal Patil
Try following code
试试下面的代码
var allRows=$('#GridFolderPopup').find('tr');
$.each(allRows,function(cntr,value){
if(cntr==0)
$(this).addClass('GridFolderRow');
});