【问题标题】:Remove specific html entity character from all labels text using jquery使用 jquery 从所有标签文本中删除特定的 html 实体字符
【发布时间】:2018-02-02 11:55:57
【问题描述】:

我有 警告标志 字符,它在 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 ⚠</label>

【问题讨论】:

  • 我在您的示例中看到并输入了没有 type="checkbox" 的内容。
  • 你也得到了字符串,分割它,加入它,然后什么都不做。该字段的 text() 不会自动更新为该新值。如果要更新,必须将其重新设置。

标签: jquery html checkbox label html-entities


【解决方案1】:

将实际字符保留在 split 函数中,而不是 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>

【讨论】:

    【解决方案2】:

    您可以使用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>

    【讨论】:

      【解决方案3】:

      您可以保留实际的 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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 2014-03-14
        • 2023-03-07
        • 1970-01-01
        • 2011-11-24
        相关资源
        最近更新 更多