gpt4 book ai didi

jQuery : find and wrap textnode with some element

转载 作者:行者123 更新时间:2023-12-01 07:35:59 29 4
gpt4 key购买 nike

我的 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>

我想找到体内任何“煽动者”并将其替换为 <span class="firebrand">firebrand</span>使用 jQuery

最佳答案

只需在 DOM 中递归,同时使用:

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

在每个文本内容上。

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"));

关于jQuery : find and wrap textnode with some element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1788939/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com