Java 删除不同类名中存在的属性
我想去掉仅存在于某些类元素中的“href”属性Java 删除不同类名中存在的属性,java,html,css,jsoup,Java,Html,Css,Jsoup,我想去掉仅存在于某些类元素中的“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 <
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 html=“这是我的示例:
.更多数据:
"
期望输出:
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>
String newhtml=“这是我的示例:
.更多数据:
保留此文本,但删除其引用
我使用JSoup来摆脱使用NewTraversor()
和trasverse
的属性,但是,它删除了所有指定的属性,我只想删除与某些类相关的属性。谢谢大家的帮助。您熟悉吗?
可以使用jQuery这样简单地完成:
jQuery('a.class2').removeAttr('href')
嗯,我不熟悉
JSoup
,但根据我发现的情况,可以按如下方式进行:
doc.select("a.class2").removeAttr("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>"
Document doc = Jsoup.parse(html, "http://example.com/");
Elements links = doc.select("a.class2");
Element link = links.first();
link.removeAttr("href");
String html=“这是我的示例:
. 更多数据:
"
Document doc=Jsoup.parse(html)http://example.com/");
元素链接=文件选择(“a.class2”);
元素链接=links.first();
link.removeAttr(“href”);
它应该可以工作。使用JSoup获取类名,检查它是否与您要查找的类相同,并且只删除匹配元素的href属性。感谢您的提示回答。实际上没有,但是如果我使用它,它会不会与JSoup(我已经在使用它解析html文档)冲突?我也从R.Oosterholt的回答中得到了同样的提示…我会试试的,谢谢!@Rodo是的,重点是他,我只是试着把它翻译成
JSoup
我在NodeTraversor()
方法中包含了这一点,效果很好。