首页 > 解决方案 > 仅删除字符串

问题描述

我正在尝试删除一串文本(通过 Jquery),除了它的父级之外没有任何标签,并且只能删除整个内容。

我需要保留标签(在 Span 内,并保留它),因为我想保留图标但删除文本,例如下面。我想删除“退出”。

我尝试使用它,但它删除了整个代码。

(function($) {
  $("a:contains('SIGN OUT')").remove("a:contains(' SIGN OUT')");
})(jQuery)
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="header-block header-link" href="account/signout" rel="nofollow">
  <span class="hidden-xd">
    <i class="fas fa-sign-out-alt"></i><br/>
  </span> SIGN OUT
</a>

非常感谢您的帮助。

标签: jquerystring

解决方案


您要做的是更改 .html 的 html a。jQuery 有一个功能 - .html().

在下面的代码示例中,我使用它来分配删除a文本后的现有 html SIGN OUT

(function($) {
  $("a:contains('SIGN OUT')").html(function() {
    return $(this).html().replace('SIGN OUT', '');
  });
})(jQuery)
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="header-block header-link" href="account/signout" rel="nofollow">
  <span class="hidden-xd">
    <i class="fas fa-sign-out-alt"></i><br/>
  </span>
  SIGN OUT
</a>


推荐阅读