测试第一个孩子

fre*_*hie 11 jquery

我正在测试表中的一行是否是第一个孩子.到目前为止,我有:

    //the loop works
$('#GridFolderPopup').find('tr').each(function () {

    if ( $(this).is(:first) == true ) //this is the faulty line
    {
         $(this).addClass('GridFolderRow');
    };
});
Run Code Online (Sandbox Code Playgroud)

有什么建议?

谢谢

Nik*_*bak 29

尝试.is(':first')而不是.is(:first).

您可能会将Javascript与Ruby混淆:在Javascript中,冒号不能像这样使用(在字符串之外).

编辑
此外,在您的情况下':first-child'选择器可能更合适.查看有关差异的文档

http://api.jquery.com/first-child-selector/
http://api.jquery.com/first-selector/

  • @frenchie你会给读者更好的印象,如果你使用否定(`!`)运算符:)`if(!$(this).is(':first-child'))` (3认同)

Mas*_*ash 7

这可能是一个解决方案:

$('#GridFolderPopup tr:first').addClass('GridFolderRow');
Run Code Online (Sandbox Code Playgroud)

也减少了很多代码.