代码之家  ›  专栏  ›  技术社区  ›  David Thomas

如何让jQuery找到第一个列表项,而不是所有列表项?

  •  2
  • David Thomas  · 技术社区  · 16 年前

    我已经成功地让列表动画左足够高兴,但我卡住了当谈到选择列表的第一个元素。我试过使用:

    $('ul#services > li:first');
    $('ul#services > li:first-child');
    $('ul#services > li').eq([0]);
    

    (下面的xhtml),

    在每种情况下 console.log(first) (the) var name used)返回所有列表项。我是不是明目张胆地做错了什么?

    li ul ,移除 并允许列表无限滚动。这只是一个服务列表,而不是链接,所以我不-目前-计划有滚动或左/右功能。

    当前xhtml:

    ( 编辑以添加当前代码/html的全部内容 )

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    
    <html>
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
        <title></title>
    
        <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
    
        <!--[if IE 8]>
    
            <link rel="stylesheet" type="text/css" href="css/ie8.css" />
    
        <![endif]-->
    
        <!--[if lte IE 7]>
    
            <link rel="stylesheet" type="text/css" href="css/ie7.css" />
    
        <![endif]-->
    
        <style type="text/css" media="screen">
    ul#services {overflow: visible; }
        </style>
    
        <script type="text/javascript" src="js/jquery.js"></script>
    
    
        <script type="text/javascript">
    
            $(document).ready(
    
                function() {
    
                    $(document).ready(function() {
    
                        $('h1 > span.businessName').each(
                                function() {
                                    var text = $(this).html();
                                    var first_letter = text.substr(0,1);
    
                                    if ( /[a-zA-Z]/.test(first_letter) ) {
                                        $(this).html('<span class="firstLetter">' + first_letter + '</span>' + text.slice(1));
                                    }
                                }
                        ); 
                });
    
                    $('ul#services > li').filter(':odd').addClass('odd');
                    $('ul#services > li').filter(':visible:last').addClass('last');
    
    //--> Function starts
    
            jQuery.fn.carousel = function() {
                // 1. find the width of the list items:
    
                    var listWidth = $('ul#services > li').outerWidth();
    
                // 2a. find the current first item:
    
                    var curFirst = $('ul#services > li').first();
    
                    console.log(curFirst);
    
                // 2b. append the current first item:
    
                // 2c. remove the first item 
    
                // 3. animate the list, to move left by listWidth:
    
                    $('ul#services > li').delay(500).css('position','relative').animate(
                        {
                        left: '-=' + listWidth
                        },
                        5000,
                            function() {
    
        //                      $('ul#services').get(0).remove();
                                $('ul#services').delay(500).carousel()
                            }
                    );
                };
    //--> Function ends.
                $('ul#services').carousel();
    
                }
            )
    
        </script>
    
    
    </head>
    
    <body>
    
        <div id="pageWrap">
    
            <div id="branding">
    
                <h1><span class="businessName">business name</span> <span class="tagline">some other info</span></h1>
                <hr id="rule">
    
            </div>
    
            <div id="content">
    
    <div class="carousel">
    
        <div class="wrapper">
    
            <ul id="services">
                <li>one</li
                ><li>two</li
                ><li>three</li
                ><li>four</li
                ><li>five</li
                ><li>six</li
                ><li>seven</li
                ><li>eight</li
                ><li>nine</li
                ><li>ten</li>
            </ul>
    
        </div>
    
            </div>
    
            <div id="mainPane">
    <p>Lorem ipsum dolor sit amet...</p>
            </div>
    
            <div id="sidebar">
    
                <div id="contact">
        <!--
            STARTS hCARD (below)
        -->
                    <div id="hcard">
                        <div id="hcard-yy-xx" class="vcard">
                            <span class="fn"><span class="given-name">YY</span> <span class="family-name">XX</span></span>
                            <div class="org">business name</div>
                            <a class="email" href="mailto:xx@xx.com">email</a>
    
                                <div class="tel">
    
                                </div>
                                <a class="downloadAs" href="http://suda.co.uk/projects/microformats/hcard/get-contact.php?uri=http://www.davidrhysthomas.co.uk/arch/tester.html">Download contact information</a>
                        </div>
                    </div>
        <!--
            ENDS hCARD (above)
        -->
                </div>
    
                <div id="professionalBodies">
    
                    <ul>
                        <li>some stuff</li>
                    </ul>
    
                </div>
    
            </div>
    
        </div>
    
            <div class="push"></div>
    
        </div>
    
            <div id="footer">
    
    
            </div>
    
    </body>
    
    </html>
    

    …我相信你想要: $('ul#services li:first').remove(); $('ul#services > li:first').delay(500)...

    使用 $('ul#services li:first').remove(); 生成错误:

    未捕获的TypeError:对象#没有“remove”方法

    而且在行为上似乎与使用 $('ul#服务>李:第一个')。延迟(500)。。。

    2 回复  |  直到 16 年前
        1
  •  9
  •   Nick Craver    16 年前

    既然你在学习,我就不做这里所有的更正了,但这里有要点:

    $('ul#services').get(0).remove();
    我相信你想要: $('ul#services li:first').remove();

    这是你看到的主要原因: $('ul#services > li').delay(500)... 这将启动上的动画 每个 <li> 您看到的是10 console.log,因为您要启动carousel 10次,每次1次 < .

    你可能想要的是 $('ul#services > li:first').delay(500)...

        2
  •  4
  •   Alex    16 年前

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

    $('ul#services li:first)
    

    是正确的,你的问题一定在别处,试试看 .first() ?