Javascript 切换不带类的替换字符/双字符

Javascript 切换不带类的替换字符/双字符,javascript,string,replace,character,Javascript,String,Replace,Character,我正在尝试替换php字符串文本中的一些字符,这些字符没有类,可能位于页面中的任何位置 我尝试过类似的方法(删除“w”字母),但它可以与p或br等一起使用,但不能与普通字符一起使用。或者给它一个要从文本中删除的字符列表,如$(“w | r | e”).toggle() 文本在不同的位置发生变化,但都在中 $(窗口)。加载(函数(){ $(文档).ready(函数(){ $(“button-hide2”)。单击(函数(){ $(“w”).toggle(); }); }); }); 这是隐藏按钮:

我正在尝试替换php字符串文本中的一些字符,这些字符没有类,可能位于页面中的任何位置

我尝试过类似的方法(删除“w”字母),但它可以与
p
br
等一起使用,但不能与普通字符一起使用。或者给它一个要从文本中删除的字符列表,如
$(“w | r | e”).toggle()

文本在不同的位置发生变化,但都在


$(窗口)。加载(函数(){
$(文档).ready(函数(){
$(“button-hide2”)。单击(函数(){
$(“w”).toggle();
});
});
});
这是隐藏按钮:

切换

这是div中任何文本的一个示例(文本随每页变化):


Lorem ipsum dolor sit amet,一位杰出的领导者,一位伟大的领袖
暂时性的劳工和财产损失


如果您至少了解文本可能所在位置的HTML结构,那么这里有一个潜在的解决方案

// Take a snapshot of the original document body
const BodyCopy = document.body.cloneNode(true);

const button = document.querySelector('#mybtn');

// Regex of text we are searching for
const textWeAreSearchingFor = new RegExp(/l|r|m/, 'ig');
// Replace them with an empty string
const replaceWith = '';
// Hang on to the original version of the HTML
const originalText = BodyCopy.querySelector('#bodytext').innerHTML;
// Manage the state on whether or not it has been toggled
let isOriginal = true;

button.addEventListener('click', () => {
  const textArea = document.querySelector('#bodytext');

  if (isOriginal) {
    const allParagraphs = document.querySelectorAll('#bodytext p');

    // Map over all of the paragraphs and replace their text
    [...allParagraphs].map(p => {
      const currentParagraphText = p.innerText;
      const updatedText = currentParagraphText.replace(textWeAreSearchingFor, replaceWith);

      p.innerText = updatedText;
    });

    isOriginal = false;
  } else {
    // Replace with original content
    textArea.innerHTML = originalText;
    isOriginal = true;
  }  
});


谢谢你的回复。不是文本从一页变到另一页。我只需要一个按钮来点击它,它会删除
@mike中任何给定文本中的一些字母-这与单独的页面无关。你能给我一个页面上潜在代码的示例,以及你可能希望单击什么来实现这一点吗?谢谢-我已经更新了我根据这个例子给出了我的解决方案。
<div id="bodytext">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
// Take a snapshot of the original document body
const BodyCopy = document.body.cloneNode(true);

const button = document.querySelector('#mybtn');

// Regex of text we are searching for
const textWeAreSearchingFor = new RegExp(/l|r|m/, 'ig');
// Replace them with an empty string
const replaceWith = '';
// Hang on to the original version of the HTML
const originalText = BodyCopy.querySelector('#bodytext').innerHTML;
// Manage the state on whether or not it has been toggled
let isOriginal = true;

button.addEventListener('click', () => {
  const textArea = document.querySelector('#bodytext');

  if (isOriginal) {
    const allParagraphs = document.querySelectorAll('#bodytext p');

    // Map over all of the paragraphs and replace their text
    [...allParagraphs].map(p => {
      const currentParagraphText = p.innerText;
      const updatedText = currentParagraphText.replace(textWeAreSearchingFor, replaceWith);

      p.innerText = updatedText;
    });

    isOriginal = false;
  } else {
    // Replace with original content
    textArea.innerHTML = originalText;
    isOriginal = true;
  }  
});