Multiclass element selection clarification [duplicate]

大城市里の小女人 提交于 2019-12-24 04:30:41

问题


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 will 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?


回答1:


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


来源:https://stackoverflow.com/questions/7563421/multiclass-element-selection-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!