开发者

Multiclass element selection clarification [duplicate]

This question already has an answer here: Jsoup div[class=] syntax works whereas div.class syntax doesn't - Why? (1 answer) Closed 9 years ago.

Assuming several multiclass divs as demonstrated in the following HTML:

<div class="class_one class_two class_three classfour classfive classsix">

<div class="class_one class_two class_three classfour classfive">

<div class="class_one class_two class_three classfour classsix">

Is there a single Jsoup select expression that wil开发者_StackOverflow社区l select all 3 of them?

To clarify, thinking that the "lowest common denominator" will select all 3, I tried the following:

div[class=class_one class_two class_three classfour] 

But it selected none!

On the other hand, using the full multiselect syntax works, but it can only select one of the above, e.g.:

div[class=class_one class_two class_three classfour classfive classsix]

Is there a way to select all 3 of them, using a single Jsoup select statement?


This is not specific to Jsoup, but to CSS. The [attribute=name] selector does an exact match. Even the ordering matters. You want to use the .classname selector here instead. The following should work:

Elements divs = document.select("div.class_one.class_two.class_three.classfour");
// ...

Note that ordering of the classnames doesn't matter here. This selector selects all <div> elements which has all of the given classnames present.

See also:

  • Jsoup selector syntax
  • Jsoup Selector API javadoc
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜