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")
Update:
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
});
}
});
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?
...
` or `...
IWANTTHIS...
` – bobthyasian Dec 16 '12 at 08:49