尝试返回父级
    时 .parents() 和 .closest() 的奇怪行为jQuery 中的元素 ID

发布于 2024-12-09 16:28:18 字数 3061 浏览 3 评论 0原文

所以我有 2 个

    容器,每个容器都有 id。它们内部是一个
  • 元素列表。

第一个

      。第二个是

每个

  • 内都有一些标签,它们有一个名为 close 的 id(这是我用作选择器的链接),这将删除每个
  • 代码>节点一旦点击。我试图定位每个
      容器以查看它来自哪里。
  • 我的 HTML 是:

                    <!-- coaches box -->
                    <div class="box">
                        <div class="heading">
                            <h3 id="coaches-heading">Coaches</h3>
                            <a id="coaches" class="filter-align-right">clear all</a>
                        </div>
                        <ul id="coaches-list" class="list">
                            <li><span>Hue Jackson<a class="close"></a></span></li>
                            <li class="red"><span>Steve Mariuchi<a class="close"></a>                     </span></li>
                        </ul>
                    </div>
    
                    <!-- players box -->
                    <div class="box">
                        <div class="heading">
                            <h3 id="players-heading">Players</h3>
                            <a id="players" class="filter-align-right">clear all</a>
                        </div>
                        <ul id="players-list" class="list">
                            <li><span>Steve Young<a class="close"></a></span></li>
                            <li><span>Gary Plummer<a class="close"></a></span></li>
                            <li><span>Jerry Rice<a class="close"></a></span></li>
                        </ul>
                    </div>
    

    我在 jQuery 中的删除标签功能是:

    function removeSingleTag() {
        $(".close").click(function() {
    
                        var $currentId = $(".close").closest("ul").attr("id");
                        alert($currentId);
    
            // find the closest li element and remove it
            $(this).closest("li").fadeOut("normal", function() {
                $(this).remove();
                return;
            });
        });
    }
    

    每当我单击每个特定标签时,它都会删除我单击的正确标签,尽管当我提醒 $currentId 时,如果我有:

                var $currentId = $(".close").closest("ul").attr("id");
    

    它会提醒“coaches-list”我在

        如果我将其更改为:

                    var $currentId = $(".close").parents("ul").attr("id");
        

        它具有与上面相同的行为,但改为提醒“players-list”。

        因此,当使用closest()时,它返回第一个

          id,但当使用parents()时,它返回最后一个
            id。

        有人知道这种奇怪的行为是怎么回事吗?

        So I've got 2 <ul> containers each with id's. Inside of them are a list of <li> elements.

        The first <ul> is <ul id="coaches-list">. The second is <ul id="players-list">.

        There are tags within each <li> that have an id called close (which is a link that I'm using as my selector), which will delete each <li> node once clicked. I'm trying to target each <ul> container to see where it is coming from.

        My HTML is:

                        <!-- coaches box -->
                        <div class="box">
                            <div class="heading">
                                <h3 id="coaches-heading">Coaches</h3>
                                <a id="coaches" class="filter-align-right">clear all</a>
                            </div>
                            <ul id="coaches-list" class="list">
                                <li><span>Hue Jackson<a class="close"></a></span></li>
                                <li class="red"><span>Steve Mariuchi<a class="close"></a>                     </span></li>
                            </ul>
                        </div>
        
                        <!-- players box -->
                        <div class="box">
                            <div class="heading">
                                <h3 id="players-heading">Players</h3>
                                <a id="players" class="filter-align-right">clear all</a>
                            </div>
                            <ul id="players-list" class="list">
                                <li><span>Steve Young<a class="close"></a></span></li>
                                <li><span>Gary Plummer<a class="close"></a></span></li>
                                <li><span>Jerry Rice<a class="close"></a></span></li>
                            </ul>
                        </div>
        

        My remove tag function in jQuery is:

        function removeSingleTag() {
            $(".close").click(function() {
        
                            var $currentId = $(".close").closest("ul").attr("id");
                            alert($currentId);
        
                // find the closest li element and remove it
                $(this).closest("li").fadeOut("normal", function() {
                    $(this).remove();
                    return;
                });
            });
        }
        

        Whenever I click on each specific tag, it's removing the proper one I clicked on, although when I'm alerting $currentId, if I have:

                    var $currentId = $(".close").closest("ul").attr("id");
        

        It alerts 'coaches-list' when I'm clicking on a close selector in both <ul id="coaches-list" class="list"></ul> and <ul id="players-list" class="list"></ul>

        If I change that to:

                    var $currentId = $(".close").parents("ul").attr("id");
        

        It has the same behavior as above, but alerts 'players-list', instead.

        So when using closest(), it's returning the very first <ul> id, but when using parents(), it's returning the very last <ul> id.

        Anyone know what is going on with this whacky behavior?

        如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

        扫码二维码加入Web技术交流群

        发布评论

        需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

        评论(3

        心舞飞扬 2024-12-16 16:28:18

        这是预期的行为。

        您应该使用:

        var $currentId = $(this).closest("ul").attr("id");
        

        $(this) 指向单击的.close

        $(".close") 指向找到的第一个。

        It's expected behavior.

        You should use:

        var $currentId = $(this).closest("ul").attr("id");
        

        $(this) points at the clicked .close.

        $(".close") points at the first one found.

        七秒鱼° 2024-12-16 16:28:18

        这是因为您从点击处理程序运行该选择器,所以您应该使用它:

        var $currentId = $(this).closest("ul").attr("id");
        

        It's because you run that selector from click handler you should use this instead:

        var $currentId = $(this).closest("ul").attr("id");
        
        酒解孤独 2024-12-16 16:28:18

        尝试使用此函数来获取父级:

        var $currentId = $(this).parents().first();
        

        我从未使用过 .closest() 函数,但根据 jQuery,您指定的应该可以工作。不管怎样,尝试一下并告诉我效果如何。

        您还需要使其通过使用 $(this) 选择当前元素

        Try using this function to get the parent:

        var $currentId = $(this).parents().first();
        

        I've never used the .closest() function but according to jQuery what you have specified should work. Either way, try that out and tell me how it goes.

        You also need to make it so that it selects the current element by using $(this)

        ~没有更多了~
        我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
        原文