使用 jquery 从所有标签文本中删除特定的 html 实体字符

Remove specific html entity character from all labels text using jquery

我有 警告标志 字符,它在 HTML 实体(十进制)中具有 ⚠ 的值,我正在尝试将其从所有复选框的标签文本。

比如在HTML中,我的其中一个输入标签如下:

我如何使用 $ (JQuery) 选择器从这些标签中删除 ⚠ html 实体。

这是我的尝试:

$('input:checkbox').each(function (index) {
  // I thought by splitting that part and then joining it
  // with an empty string would work.
  $(this).next().text().split('⚠').join('');
});
<input id='input1'/><label for='input1'>This is the warning sign &#9888;</label>

您可以使用 split()slice(),然后使用 join() 来实现此目的,假设这些字符始终位于字符串的末尾:

$(document).ready(function() {
  $('input:checkbox').each(function(index) {
    var yourText = $(this).next().text();
    var convertedText = yourText.split(' ').slice(0, -1).join(' ');
    console.log(convertedText);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' id='input1' /><label for='input1'>This is the warning sign &#9888;</label>

在拆分函数中保留实际字符而不是 html 实体。

$('input:checkbox').each(function(index){       
  $(this).next().text($(this).next().text().split('⚠').join(''));
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id='input1' type="checkbox"/><label for='input1'>This is the warning sign &#9888;</label>

您可以保留实际的 HTML 个字符:

$('input:checkbox').each(function(index){       
  $(this).next().text($(this).next().text().replace('⚠',''));
}); 
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<input id='input1' type="checkbox"/><label for='input1'>This is the warning sign &#9888;</label>