JavaScript/jQuery: replace part of string?

后端 未结 2 1424
栀梦
栀梦 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);
    });
    
    
    N/A, Category


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

    $(".element span").text(function(index, text) {
        return text.replace("N/A, ", "");
    });
    

提交回复
热议问题