質問

using jQuery I need to dynamically find the next occurrence of a given element in the DOM by its class name regardless of the DOM structure. This means the elements in question could be scattered at random within the DOM with no common parent element. Additional elements could be added to the DOM at any time post page load so I need to avoid getting a list of the elements on page load and iterating through that. Any ideas?

役に立ちましたか?

解決

Use the current elements' index(), passing the collections' selector, and add 1 to get the next occurrence:

var index = $('div.example:first').index('div.example') + 1;
var $next = $('div.example').eq(index);

I had the same question a while back

Here's a fiddle

他のヒント

var givenElement =$('.myClass').eq(0);


//Next Occ
givenElement.next('.myClass')

DEMO : http://jsfiddle.net/abdennour/Q9835/3/

Try ,

console.log($("body").find(".yourclassname"));

You can use .nextAll() with :first this way:

$('.cls').nextAll('.cls:first')

where .cls is your class name of your dom elem.

Demo

Another Demo

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top