开发者

Combine multiple selector with filter

开发者 https://www.devze.com 2023-01-21 22:52 出处:网络
Is there a way to combine a multiple selector and a basic filter within a single query? For example...

Is there a way to combine a multiple selector and a basic filter within a single query?

For example...

var jq = $(someElement);

// Want to find the first following sibling node which has either classA or classB.

// This imaginary jquery does not quite work.
jq.nextAll("(.classA, .classB):first") 
开发者_开发问答
// This works. But I wonder if I can achieve the same result with just one query.
jq.nextAll(".classA, classB)").filter(":first")


what if you do .first() instead of .filter(":first") ?

Does that help ?


not sure exactly what you're trying to do, but if you just wanted the innerhtml of the first element you should be able to do something like:

$(".classA, .classB", someElement).html();

since html will act on the first matched element.

Otherwise, i don't see what is wrong with your method that works... I suppose you're just asking for additional knowledge?

I never find much need for the :first selectorbut i would probably do this like:

$(".classA, .classB", someElement).first();


Well im pretty sure this is the same thing and uses only one query:

jq.nextAll('.classA:first, .classB:first');

Obviously its mroe verbose but unless there is something special about the :first selector that should work.


Try this:

var first = jq.nextAll('.classA, .classB').first();

Working example here.

0

精彩评论

暂无评论...
验证码 换一张
取 消