如何替换元素?
我有以下 HTML
<html>
<head>
<title>test</title>
</head>
<body>
<table>
<caption>table title and/or explanatory text</caption>
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td id=\"test\" width=\"272\"></td>
</tr>
</tbody>
</table>
<a href=\"http://www.google.fi\" style=\"color:black\">Test link</a>
<a href=\"http://www.google.fi\"><img src=\"http://www.google.se/images/nav_logo95.png\" /></a>"
</body>
</html>;
我想找到第一个带有 jsoup 的链接并将其替换为文本
Element elem = page.select("a[href=" + link.getUrl() + "]:contains(" + link.getName() + ")").first();
我只能用 elem.html("foo")
替换内部 HTML 或用 < 打印外部 Html code>elem.outerHtml()
有谁知道我如何实现这一目标?
I have the following HTML
<html>
<head>
<title>test</title>
</head>
<body>
<table>
<caption>table title and/or explanatory text</caption>
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td id=\"test\" width=\"272\"></td>
</tr>
</tbody>
</table>
<a href=\"http://www.google.fi\" style=\"color:black\">Test link</a>
<a href=\"http://www.google.fi\"><img src=\"http://www.google.se/images/nav_logo95.png\" /></a>"
</body>
</html>;
And I want to find the first link with jsoup and replace it with a text
Element elem = page.select("a[href=" + link.getUrl() + "]:contains(" + link.getName() + ")").first();
I can only replace the inner HTML with elem.html("foo")
or print the outerHtml with elem.outerHtml()
Does anyone know how I can achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了答案!
I found the answer!
找到要使用的元素后,您可以应用如下所述的命令: http://jsoup.org/cookbook/modifying-data/set-html
我无法正确理解。我正在尝试这个:
但我得到这个:
我仍然不知道交换 URL 元素内容的确切方法。
我想得到的结果是:
我怎样才能删除中间的 href?
Once you have found the element that you want to work with, you may apply the commands such as explained here: http://jsoup.org/cookbook/modifying-data/set-html
I could not get it right. I am trying this:
But I am getting this:
I still don´t know the exact way to exchange the contents of an URL element.
I´d like to have, as the result:
How coul´d I erase the href that is inbetween?