jquery - 仅删除字符串
问题描述
我正在尝试删除一串文本(通过 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>
非常感谢您的帮助。
解决方案
您要做的是更改 .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>
推荐阅读
- r - 在 R 中找到一个截止值
- java - 如何禁用 Spring 执行器的内容协商?
- android - Android更改默认权限保护级别
- c# - 并行运行计算繁重任务而不等待 C# 中的结果
- python-3.x - Python unittest 未从指定目录运行
- dns - 具有自定义域的 G-Suite 电子邮件也用于在 Digital Ocean 托管。如何?
- c++ - 用 C++ 实现的神经网络的可疑预测
- angular - 如何通过 ng-content 将异步数据传递给子组件
- css - 当我将光标从按钮上移开时如何使悬停生效
- grafana - 批量插入数据的 Grafana 警报