从帖子链接中删除页面名称

时间:2021-01-03 15:18:02

标签: jquery text replace href

所以,我有一个可以在主题中发布链接的论坛。但我只希望我的链接,而不是主题标签链接从 https://{FORUMNAME}.com/extensions 更改为简单的 /extensions

帖子的 HTML 向您展示示例:

<div class="content">
  <a href="/tags/welcome" rel="nofollow">#Welcome</a>
  <a href="/">https://{FORUMNAME}.com/</a>
  <a href="/t20-topic">https://{FORUMNAME}.com/t20-topic</a>
  <a href="/shop">https://{FORUMNAME}.com/shop</a>
</div>

我希望它显示的是这样的链接:

#Welcome
https://{FORUMNAME}.com/
/t20-topic
/shop

但如果 https://{FORUMNAME}.com// 取代,它也会起作用。

我希望使用 jQuery 来完成。

任何帮助将不胜感激。

谢谢。

编辑:

所以我设法在我的论坛上找到了一种方法,但我想知道是否有更简单的方法来做到这一点。

这是我使用的脚本:

  $('.content a').addClass('postLinks');
  $('.content a[rel="nofollow"]').removeClass('postLinks');
  
  $('.content a.postLinks').text(function(){        
    $(this).text($(this).text().replace('https://{FORUMNAME}.com',''));
  });
  
  $('a.postLinks[href="/"]').text('/home');

问候。

0 个答案:

没有答案