Friday 15 July 2011

jQuery find next/prev elements of a certain class but not necessarily siblings -


Next, previous, next, now and all previous methods are very useful, but if you are not trying to find the elements The key element I want to do is something like this:

  & lt; Div & gt; & Lt; Span id = "click" & gt; Hello & lt; / Span & gt; & Lt; / Div & gt; & Lt; Div & gt; & Lt; P class = "search" & gt; World & gt; & Lt; / P & gt; & Lt; / Div & gt;  

When click on ID , I want to match the class from the next element to Find , in which case The click elements are not brothers or will not work.

I was working on it today's own problem, here it is that with whom I came

  / ** * Find the next element that matches a certain selector is different from * in the next () * that searches for the parent of the current element * Find the selector for the ultimate selector * @latest steps (optional) to find the number of steps, default 1 * @ in the absolute field (optional) Jais, the default document extensive * / $ is Fn.findNext = function (selector, move, scope) given {// move? Then pars (step) for integer {steps = Math.floor (steps); } And if (step === 0) {// stupid case :) it's back; } Else {// Other, try var next = this.next (selector); If (next.length) return forward; // FAST FAIL FAIL, STRIPLY STEP :) STEPS = 1; } // set scope for document or user-defined area = (scope)? $ (Area): $ (document); // Find the children who are matched with the selector: children used to serve as exclusions = this.find (selector); // search in parent (s) = $ (this); While (grass [0]! = Scope [0]) // // Grass of a level above = hay.parent (); // Except children of the current element - / / - Exclude the children of the current element - Add the current element (document will be added in sequence) var rs = hay.find (selector) .not (children) .add ($ (this) ); // var id = rs.index (this) + move the desired number of steps; // results found? Then return (ID & gt; -1 & amp; amp; ID & lt; rslength) Refund $ (Rs [ID]); } // Return blank results return $ ([]); }  

in your example

   

You can now

  $ ('# click')). FindNext ('find.example.com). Html ('test 123');  

I doubt it will perform well on large structures, but here it is :)


No comments:

Post a Comment