jQuery next()什么也不返回 [英] jquery next() returning nothing

查看:191
本文介绍了jQuery next()什么也不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图选择相邻的<p>之间的所有内容,"p"的数量每次都会改变.在p个标签对之间的内容可能毫无意义.像这样的东西:

I was trying to select everything between adjacent <p>'s, the number of "p" changes every time. And contents in between p tag-pairs could be nothing to anything. Some thing like this:

    <p><a href="x">...ABC...</a></p>

    <<<<<<<< Beginning of what I want >>>>>>>>

    <fieldset>...</fieldset>
    <font title="...">...</font>
    sometext without any tag<br>
    <a href="...">...</a>
    //[0..N] more tags

    <<<<<<<< End of what I want >>>>>>>>

    <p><a href="x+1">...ABC...</a></p>
    [0..N] more "p"'s with similar pattern ("p" with random url in "a")

更新:

我想将这些流氓代码(未标记的文本)包装到某个div中,以便以后进行处理.像这样:

I want to wrap those rogue codes (untagged text) into some div so that I can process them later. Like this:

<div id="outer">
    <div id="1">
        <p><a href="x">...ABC...</a></p>

    <! Beginning of what I want >

        <fieldset>...</fieldset>
        <font title="...">...</font>
        sometext without any tag<br>
        <a href="...">...</a>
        //[0..N] more tags

    <! End of what I want >
    </div>
    <div id="2">
        <p><a href="x+1">...ABC...</a></p>
    </div>
    <div id="3">
        //something or nothing
    </div>
    //something or nothing
</div>

为此,我必须使用此代码,因为其中有些文本周围没有任何标签:

In order to do that, I had to this code because there is some text without any tags around it:

    var ps = $("p:contains('ABC')");
    ps.each(function(){
        if(!($(this).next()[0])){
            return true;
        }
        var me = $(this);
        var pa = me.parent().contents();
        var nx = me.next("p:contains('ABC')"); //returns [] in this case
        var i0 = pa.index(me);
        var i1 = pa.index(nx);
        if (i1 > i0) {
            var elements = pa.slice(i0, i1);
            elements.each(function(){
                //Do something
            });
        }
    }); 

如代码中标记的那样,即使我将其更改为next("p"),next()函数也不会返回任何内容.但是,如果我使用me.next().next().next().next().next(),则可以选择下一个"p"标记.为什么会这样?我怎样才能做得更好?

As marked in the code, the next() function would not return anything even if I change it to next("p"). But if I use me.next().next().next().next().next() I can select the next "p" tag. Why would this happen? How could I do it better?

推荐答案

您应该为此目的使用.nextUntil("p")!因为.next()仅检查下一个元素,nextUntil()一直搜索直到找到关联的元素!

You Should Use .nextUntil("p") for this purpose! because .next() only checks for the next element and nextUntil() searches until an associated element found!

这篇关于jQuery next()什么也不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆