移除指定标签并保留内容

移除指定标签(如 <b> 和 <strong>)并保留其内容

  1. // ==UserScript==
  2. // @name 移除指定标签并保留内容
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 移除指定标签(如 <b> 和 <strong>)并保留其内容
  6. // @author Your Name
  7. // @match *://xueqiu.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // 定义需要移除的标签
  16. const tagsToRemove = ['b', 'strong'];
  17.  
  18. // 遍历页面中的所有需要移除的标签
  19. tagsToRemove.forEach((tag) => {
  20. const elements = document.querySelectorAll(tag);
  21. elements.forEach((element) => {
  22. // 创建一个文档片段,用于存储标签内的内容
  23. const fragment = document.createDocumentFragment();
  24. while (element.firstChild) {
  25. fragment.appendChild(element.firstChild);
  26. }
  27. // 将文档片段插入到父元素中,替换掉原来的标签
  28. element.parentNode.insertBefore(fragment, element);
  29. element.remove(); // 移除标签
  30. });
  31. });
  32.  
  33. console.log('已移除指定标签并保留内容');
  34. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址