多类元素选择说明 [英] Multiclass element selection clarification
问题描述
假设如以下HTML所示,几个多类div:
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">
是否有一个Jsoup select表达式可以选择全部三个?
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]
但是它选择了无!
另一方面,使用完整的多选语法也可以,但是只能选择其中一种,例如:
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]
是否有一种方法可以使用 single Jsoup select语句选择全部3个?
Is there a way to select all 3 of them, using a single Jsoup select statement?
推荐答案
这不是特定于Jsoup,而是CSS. [attribute=name]
选择器进行完全匹配.甚至订购也很重要.您想在这里使用.classname
选择器.以下应该可以工作:
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");
// ...
请注意,此处的类名顺序无关紧要.该选择器选择所有存在所有给定类名的所有<div>
元素.
Note that ordering of the classnames doesn't matter here. This selector selects all <div>
elements which has all of the given classnames present.
- Jsoup selector syntax
- Jsoup
Selector
API javadoc
这篇关于多类元素选择说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!