我想摆脱只存在于某些类元素中的“href”属性。
String html="<div>This is my example:
<a class="class1" href="www.example.com">Hello World</a>. More data:
<a class="class2" href="www.nuisance.com">
Keep this text but remove its reference
</a></div>"
期望的输出:
String newhtml="<div>This is my example:
<a class="class1" href="www.example.com">Hello World</a>. More data:
<a class="class2">
Keep this text but remove its reference
</a></div>
我使用JSoup来删除使用NewTraversor()
和trasverse
的属性,但是,它会删除指定的所有属性,并且我只想删除与某些类关联的属性。谢谢大家的帮助。
答案 0 :(得分:1)
您熟悉jQuery吗? 它可以简单地使用jQuery完成:
jQuery('a.class2').removeAttr('href')
答案 1 :(得分:0)
好吧,我不熟悉JSoup
,但根据我发现here的内容,可以按照以下方式完成:
doc.select("a.class2").removeAttr("href");
答案 2 :(得分:0)
你可能需要这样的东西 -
String html="<div>This is my example:
<a class="class1" href="www.example.com">Hello World</a>. More data:
<a class="class2" href="www.nuisance.com">
Keep this text but remove its reference
</a></div>"
Document doc = Jsoup.parse(html, "http://example.com/");
Elements links = doc.select("a.class2");
Element link = links.first();
link.removeAttr("href");
它应该有用。