JQuery replace all text for element containing str

2019-09-07 16:59发布

I am trying to select all elements on the page which have the string 'aggr' as part of their id value. Then I am trying to replace the text 'Sum 'in these elements to 'Total'.

My JQuery is below but doesn't seem to be working....

$('TBODY[id*=aggr]').each(function(i){
var aggrHTML = $('TBODY[id*=aggr]');
          var aggrText = aggrHTML.text();
          var newText = aggrText.replace("Sum","Total Holiday Leave")
         aggrHTML.html(newText);
});

2条回答
再贱就再见
2楼-- · 2019-09-07 17:23

Try something like this

$('TBODY[id*=aggr]').each(function(i){
          var aggrText = $(this).text();
          var newText = aggrText.replace("Sum","Total Holiday Leave")
          $(this).html(newText);
});

I think that will work.

Basically you it iterates over a set of objects, but you have to use $(this) to get the current object.

I left it the way you had it so you can understand it more.

查看更多
地球回转人心会变
3楼-- · 2019-09-07 17:37

Try this:

$('TBODY[id*=aggr]').each(function(i){
  $(this).html($(this).html().replace("Sum","Total Holiday Leave"));
});
查看更多
登录 后发表回答