我如何用javascript替换在一个类下发现的许多地方的一个单词

how do I replace one word found many places under one class with javascript?

本文关键字:一个 发现 许多地 单词 替换 javascript 何用      更新时间:2023-09-26

我正在写一个chrome扩展来重新设计一个项目的tumblr仪表板。

这是一件小事,但一直非常令人沮丧。我想去掉"备注"这个词,在每个帖子下面的备注数后面。

字符串在。note_link_current类下,所以我尝试了

var textNotes = $('.note_link_current').text();
textNotes = textNotes.replace('notes', ' ');
$('.note_link_current').text(textNotes);

似乎发生的是,我得到了所有帖子的所有笔记的组合字符串。我怎样才能让所有人都受到影响呢?

现在我得到这样的东西:

251 
                63 notes441 notes74 notes851 notes49,263 notes2,561 notes142 notes1 note651 notes 

任何帮助都将非常感激!

@FelixKling说的话,但有更多的解释:

$('.note_link_current')这样的jQuery选择器将返回一个节点数组。

您正在尝试对这个数组进行操作,就像它是一个节点一样。

相反,你应该做的是调用.each(),对每个元素执行一个操作。

$('.note_link_current').each( function(index) {
  // Inside this function, "this" refers to the (DOM) element selected
  var text = $(this).text();
  /* ... */
});