简体   繁体   English

如果有兄弟姐妹,则 jquery 兄弟姐妹()似乎包括选择器,但如果没有则不包括选择器

[英]jquery siblings() seems to include selector if there are siblings but not if not

I have an ajax call with the following success function:我有一个具有以下成功功能的ajax调用:

            success: function(response){
                console.log($(response).siblings());
                $(response).siblings().each(function(){
                    alert("foo");
                    var thing_id = $(this).attr("id").split("-")[0];
                    alert(thing_id);
                    $("#"+thing_id).append(this);
                })

where response is rendered html using this django template (slightly anonymized):使用此 django 模板(稍微匿名)将响应呈现为 html:

{% for thing in things %}
    <div id="{{ thing.something.id }}-pc">
        <div>
            <span class="tablename">Something Else</span>
            {% include '<some template>' with table=thing.something_else %}
        </div>

    </div>
{% endfor %}

the docs for sibling() state that the selector is not included, which is what I see if I only have one thing in things in my template, but if I have more than one, I get both top level divs alerted in the loop.兄弟() 的文档声明不包含选择器,如果我的模板中只有thing in things ,我就会看到这thing in things ,但如果我有多个,我会在循环中收到两个顶级 div 的警报。

What's going on?!这是怎么回事?!

EDIT (better question): why am I getting both top level divs when there are 2, but 0 when there is only 1, when I call siblings() on $(response) ?编辑(更好的问题):为什么当我在$(response)上调用siblings()时,当有 2 个时我得到两个顶级 div,而当只有 1 个时得到 0?

Seems like I'm misunderstanding what I'm selecting with $(response) .似乎我误解了我用$(response)选择的内容。 Can anyone clarify?任何人都可以澄清吗?

"So what am I selecting when I say $(response) ?" “那么当我说$(response)时我在选择什么?”

That gives you a jQuery object that includes all of the top-level elements in your response string.这为您提供了一个包含response字符串中所有顶级元素的 jQuery 对象。 If there were, say, three top-level elements then $(response).length will be 3 .如果有,比如说,三个顶级元素,那么$(response).length将是3

$(response).siblings() then gives you all of the siblings of all of the top-level elements, and because each is a sibling of the other(s) this again returns all of the top-level elements. $(response).siblings()然后为您提供所有顶级元素的所有兄弟元素,并且因为每个元素都是其他元素的兄弟元素,因此再次返回所有顶级元素。

If there is only one top-level element then it has no siblings.如果只有一个顶级元素,那么它就没有兄弟元素。

So obviously .siblings() is the wrong method to be using here, but I'm not sure what to advise you to use instead because your desired result is still unclear.所以很明显.siblings()是在这里使用的错误方法,但我不确定建议您改用什么,因为您想要的结果仍然不清楚。

You can loop through each top-level element with:您可以使用以下命令遍历每个顶级元素:

$(response).each(function() {...

To get a particular one you can use .eq(someZeroBasedIndex) , eg, $(response).eq(1) to get the second.要获得特定的一个,您可以使用.eq(someZeroBasedIndex) ,例如, $(response).eq(1)来获得第二个。

To get all except the first you can use $(response).slice(1) .要获得除第一个之外的所有内容,您可以使用$(response).slice(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM