JavaScript/jQuery: replace part of string?

后端 未结 2 1415
栀梦
栀梦 2020-12-12 20:51

With text like this:

N/A, Category

I want to get rid of every occurrenc

相关标签:
2条回答
  • 2020-12-12 21:18

    You need to set the text after the replace call:

    $('.element span').each(function() {
      console.log($(this).text());
      var text = $(this).text().replace('N/A, ', '');
      $(this).text(text);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="element">
      <span>N/A, Category</span>
    </div>


    Here's another cool way you can do it (hat tip @Felix King):

    $(".element span").text(function(index, text) {
        return text.replace("N/A, ", "");
    });
    
    0 讨论(0)
  • 2020-12-12 21:30

    It should be like this

    $(this).text($(this).text().replace('N/A, ', ''))
    
    0 讨论(0)
提交回复
热议问题