【问题标题】:How to get jQuery to find the first list-item, rather than all list-items?如何让 jQuery 找到第一个列表项,而不是所有列表项?
【发布时间】:2011-02-01 06:17:11
【问题描述】:

我正在尝试实现一个基本的 jQuery 无限轮播。学习过程和其他任何事情一样多(通常我不喜欢重新发明轮子,但是......我必须以某种方式学习,不妨从基础开始)。

我已经成功地让列表向左移动了,但在选择列表的第一个元素时我被卡住了。我试过用:

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

(下面的xhtml),

在每种情况下,console.log(first)(使用的 var 名称)都会返回所有列表项。我是不是在公然做某事,而且显然是错误的?

最终的计划是克隆第一个li,将其附加到父ul,从列表中删除li,并允许列表无限滚动。它只是一个服务列表而不是链接,所以我目前不打算拥有滚动或左/右功能。

当前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>

已更新,以回应 Nick Craver 的回答:

...我相信你想要:$('ul#services li:first').remove(); ...你可能想要的是$('ul#services &gt; li:first').delay(500)...

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

Uncaught TypeError: Object # has no method 'remove'

而且在行为上似乎与使用 $('ul#services &gt; li:first').delay(500)... 没有区别

【问题讨论】:

  • 你先在哪里设置?选择器是正确的,我猜问题出在外面,你能把代码贴在你设置/使用它的地方吗?
  • 我希望隐藏我的 - 老实说 - 对 jQuery 的残暴罪行...... =) 是的,我将发布我试图利用的功能/采用。给我几秒钟...
  • 你能提供完整的代码吗?那里可能有一些东西可以帮助我们。
  • @Nick Craver,@NickLarson:完成。

标签: jquery xhtml jquery-selectors css-selectors


【解决方案1】:

既然是学的,这里就不一一指正了,主要是以下几点:

这是错误的:$('ul#services').get(0).remove();
相信你想要:$('ul#services li:first').remove();

这是您看到的主要原因:$('ul#services &gt; li').delay(500)... 这会在子 &lt;li&gt;每个 上启动动画,您看到的是 10 个 console.log,因为您启动轮播 10 次,每个 &lt;li&gt; 1 个。

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

所有这些都有意义吗?我不确定你想要最终结果是什么,只是想解释一下你目前的行为。

【讨论】:

  • 啊!感谢 Google 的控制台,我发现了 remove() 错误。我什至没有想到...动画问题。谢谢!那么,我假设我需要为ul 而不是后代lis 制作动画(我在评论中留下了我的错误,但编辑注意我已经注意到你的建议重新制作动画$('ul#services > li:first')` 再次感谢?+1
  • @ricebowl - 我不确定你想要的结果是什么,很难说,你想让他们向上滚动还是......?
  • 向左滚动。理想情况下,我试图克隆第一个列表元素,将其附加到列表中,然后删除克隆的元素。冲洗并重复...
  • 顺便说一句,使用$('ul#services').get(0).remove() 会在 Chrome 的控制台中生成错误:“Uncaught TypeError: Object # has no method 'remove'”。
  • 除了从右到左的滚动首选项,这正是我所追求的 =) 谢谢! +接受!
【解决方案2】:

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

$('ul#services li:first)

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

【讨论】:

  • 假设你的意思是var first = $('ul#services &gt; li').first() 我只能说它没有用。
猜你喜欢
  • 2022-10-12
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
相关资源
最近更新 更多