简体   繁体   English

使用Jsoup删除Id属性的div?

[英]Remove div by the Id attribute using Jsoup?

I have a query like I can remove the entire label identifying the Div but Div Id attribute, because the code that I have only do by the order of Div. 我有一个查询,比如我可以删除标识Div但Div Id属性的整个标签,因为我只按Div的顺序执行的代码。

String baseHtml = "<div id='stylized' class='myform'>"
                 + "<input id='txt_question' name='preg' type='text' disabled='disabled' style='width:150px;'>"
                 + "<div id='detail_question'>Rock</div></div>";

Document doc = Jsoup.parse(baseHtml);
Elements elements = doc.select("div");
elements.get(1).remove();
elements = doc.select("div");
System.out.println(elements);

Thanks a lot. 非常感谢。

String baseHtml = "<div id='stylized' class='myform'>"
        + "<input id='txt_question' name='preg' type='text' disabled='disabled' style='width:150px;'>"
        + "<div id='detail_question'>Rock</div></div>";

Document doc = Jsoup.parse(baseHtml);
doc.getElementById("detail_question").remove();
Elements elements = doc.select("div");
System.out.println(elements);

This line will return the element with id detail_question. 该行将返回id为detail_question的元素。

doc.getElementById("detail_question")

Element can be removed, so with chaining you have 元素可以删除,所以链接你有

doc.getElementById("detail_question").remove()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM