如何从一组元素中删除一个元素? [英] How to remove an element from a group of elements?
本文介绍了如何从一组元素中删除一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下代码:
HTML:
<div>a</div>
<div>b</div>
<div>c</div>
JS:
Document doc = Jsoup.parse(baseHtml);
Elements elements = doc.select("div");
elements.get(1).remove();
// remove doesn't affect elements. why?
elements.size(); // equal 3
// but this works
doc.outerHtml() // <div>a</div><div>c</div>
我是否必须使用此代码来获取已删除的元素?看来太冗长了.
Do I have to use this code to get removed elements? It seems too verbose.
Document doc = Jsoup.parse(baseHtml);
Elements elements = doc.select("div");
elements.get(1).remove();
elements = doc.select("div");
推荐答案
这应该对您有帮助:
public class TestJsoup {
public static void main(String[] args) {
try {
Document doc = Jsoup.connect("http://www.google.ro").get();
Elements select = doc.select("div");
System.out.println(select.size());
select.remove(1);
System.out.println(select.size());
} catch (IOException ex) {
Logger.getLogger(TestJsoup.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这篇关于如何从一组元素中删除一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文