jQuery filters prohibited words that appear in the body tag

Traverse <body>the text nodes in the entire label, replace the forbidden words, and update the text content of the nodes. Replace <body>occurrences of prohibited words throughout the tag.

Create a new forbiddenWords.js file

// 定义要替换的违禁词和替换文本
var forbiddenWords = ['违禁词1', '违禁词2'];
var replacementText = ' '; // 替换后的内容

// 递归函数:遍历元素及其子元素并替换违禁词
function replaceForbiddenWords(element) {
  $(element).contents().each(function() {
    // 判断当前节点类型
    if (this.nodeType === Node.TEXT_NODE) {
      // 替换违禁词
      var originalText = this.textContent;
      var replacedText = originalText;
      $(forbiddenWords).each(function(index, word) {
        replacedText = replacedText.replace(new RegExp(word, 'gi'), replacementText);
      });
      // 更新节点文本内容
      if (replacedText !== originalText) {
        this.textContent = replacedText;
      }
    } else if (this.nodeType === Node.ELEMENT_NODE) {
      // 递归处理子元素
      replaceForbiddenWords(this);
    }
  });
}

// 替换整个<body>标签内的违禁词
replaceForbiddenWords('body');

just import

 <script src="forbiddenWords.js"></script>

Guess you like

Origin blog.csdn.net/weixin_44523517/article/details/131433798
Recommended