如何替换元素?

发布于 2024-12-21 11:07:43 字数 937 浏览 2 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2024-12-28 11:07:44

我找到了答案!

TextNode text = new TextNode("foo", "");
elem.replaceWith(text);

I found the answer!

TextNode text = new TextNode("foo", "");
elem.replaceWith(text);
走过海棠暮 2024-12-28 11:07:44

找到要使用的元素后,您可以应用如下所述的命令: http://jsoup.org/cookbook/modifying-data/set-html

我无法正确理解。我正在尝试这个:

elemento.prepend("<a href='www.test.com'>");    
elemento.html("Roberto C. Santos.");                
elemento.append("</a>");
elemento.wrap("<a href='www.test.com'> </a>");  

但我得到这个:

<td><a href="www.test.com"> <a style="" target="_self" title="" href="http://ivv.veveivv.vvzenes.com.br/mao/ara/ccacao" data-mce-href="resolveuid/5cc1c7c8c9efcacaaeedec22a9c69a54" class="internal-link">Roberto C. Santos.</a></a></td>
  </tr>

我仍然不知道交换 URL 元素内容的确切方法。

我想得到的结果是:

<a href='www.test.com'> Roberto C. Santos.</a>" 

我怎样才能删除中间的 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:

elemento.prepend("<a href='www.test.com'>");    
elemento.html("Roberto C. Santos.");                
elemento.append("</a>");
elemento.wrap("<a href='www.test.com'> </a>");  

But I am getting this:

<td><a href="www.test.com"> <a style="" target="_self" title="" href="http://ivv.veveivv.vvzenes.com.br/mao/ara/ccacao" data-mce-href="resolveuid/5cc1c7c8c9efcacaaeedec22a9c69a54" class="internal-link">Roberto C. Santos.</a></a></td>
  </tr>

I still don´t know the exact way to exchange the contents of an URL element.

I´d like to have, as the result:

<a href='www.test.com'> Roberto C. Santos.</a>" 

How coul´d I erase the href that is inbetween?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文