jQuery:查找并用某些元素包装文本节点

发布于 2024-08-12 09:38:01 字数 458 浏览 6 评论 0原文

我的 HTML 代码:

<body>Firebrand will tend to burn any bricks out there. so the best idea is to ignore any active firebrand if you are a bricks. Otherwise, you can challenge a firebrand if you have the proper quality to keep up with their latest technology. And don't mess up with firebrand if you are a robber.</body>

我想找到正文中的任何“firebrand”,并将其替换为 firebrand 和 jQuery

my HTML code :

<body>Firebrand will tend to burn any bricks out there. so the best idea is to ignore any active firebrand if you are a bricks. Otherwise, you can challenge a firebrand if you have the proper quality to keep up with their latest technology. And don't mess up with firebrand if you are a robber.</body>

I want to find any "firebrand" inside the body and replace it with <span class="firebrand">firebrand</span> with jQuery

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-08-19 09:38:01

只需递归遍历 DOM,同时

使用: .replace(/(firebrand)/gi, '$1')

在每个文本内容上

function firebrand($el) {
   $el.contents().each(function () {

       if (this.nodeType == 3) { // Text only
           $(this).replaceWith($(this).text()
               .replace(/(firebrand)/gi, '<span class="firebrand">$1</span>'));
       } else { // Child element
           firebrand($(this));
       }

   });
}

firebrand($("body"));

Just recurse through the DOM, while using:

.replace(/(firebrand)/gi, '<span class="firebrand">$1</span>')

on each text content.

function firebrand($el) {
   $el.contents().each(function () {

       if (this.nodeType == 3) { // Text only
           $(this).replaceWith($(this).text()
               .replace(/(firebrand)/gi, '<span class="firebrand">$1</span>'));
       } else { // Child element
           firebrand($(this));
       }

   });
}

firebrand($("body"));
雄赳赳气昂昂 2024-08-19 09:38:01

尝试以下方法。

var textApply;
textApply = function(node){
  jQuery.map($(node), function(node) {
    var value = $(node)[0];
    if (value.nodeType == 3 && value.textContent.match(/firebrand/gi)) {
      var foo = value.textContent.replace(/[\n\r\t ]+/,' ').replace(/firebrand/gi, '<span style="color:red">Firebrand</span>');
      var newitem = $(foo);
      $(node).replaceWith(newitem);
    } else if (value.nodeName != 'SCRIPT') {
      jQuery.map($(node).contents(), textApply);
    }
  });
};
textApply($('body'));

Try out the following.

var textApply;
textApply = function(node){
  jQuery.map($(node), function(node) {
    var value = $(node)[0];
    if (value.nodeType == 3 && value.textContent.match(/firebrand/gi)) {
      var foo = value.textContent.replace(/[\n\r\t ]+/,' ').replace(/firebrand/gi, '<span style="color:red">Firebrand</span>');
      var newitem = $(foo);
      $(node).replaceWith(newitem);
    } else if (value.nodeName != 'SCRIPT') {
      jQuery.map($(node).contents(), textApply);
    }
  });
};
textApply($('body'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文