替换多个元素中字符的所有实例

Replace all instances of a character in multiple elements

本文关键字:实例 字符 元素 替换      更新时间:2023-09-26
<div class="example">Some example text</div>
<div class="example">Some more example text</div>

如何在没有预先存在的 id 的情况下删除上述元素中字母"e"的所有实例(不需要区分大小写(?

$('div').each(function() {
    this.innerHTML= this.innerHTML.replace(/e/g, '');
});

现场演示

如果你想要eE使用它:

$('div').each(function() {
    this.innerHTML= this.innerHTML.replace(/e|E/g, '');
});

如果这些<div>中有元素,请使用 text 函数仅获取 textNodes:

$('div').each(function() {
    $this = $(this);
    $this.text($this.text().replace(/e|E/g, ''));
});

假设你想替换内部文本的内容(而不是一般地替换标签内的所有'e'(:

$(yourselector).each(function(){
  $(this).text($(this).text.replace(/e/g, ''));
});

附言在gdoron评论后更正...

var de = document.documentElement;
de.innerHTML = de.innerHTML.replace(/>.*?</g, function(a, b) {
    return a.replace(/e/g, "f");
});