English 中文(简体)
测试第一个孩子
原标题:test if first child
  • 时间:2011-02-16 04:54:15
  •  标签:
  • jquery

我正在测试表中的一行是否是第一个子项。到目前为止,我已经:

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

    if ( $(this).is(:first) == true ) //this is the faulty line
    {
         $(this).addClass( GridFolderRow );
    };
});

有什么建议吗?

谢谢

最佳回答

这可能是一个解决方案:

$( #GridFolderPopup tr:first ).addClass( GridFolderRow );

同时也大大减少了代码。

问题回答

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

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

edit
Also, in your case :first-child selector might be more appropriate. See the docs about the difference

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

简单地询问/tbody的第一个子项比测试其中的每一行效率高得多。表越大,差异就越大。

因此:

  • 如果您想“查看表中的[give]行是否是第一个子行”

    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

尝试以下代码

var allRows=$( #GridFolderPopup ).find( tr );
$.each(allRows,function(cntr,value){
            if(cntr==0)
                 $(this).addClass( GridFolderRow );
});
$( #GridFolderPopup ).find( tr ).eq(0).addClass( GridFolderRow );




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签