当前的jQuery如下所示:
jQuery(function($) {
$('body :not(script,sup)').contents().filter(function() {
return this.nodeType === 3;
}).replaceWith(function() {
return this.nodeValue.replace(/[®]/g, '<sup>$&</sup>');
});
});
这是StackOverflow问题,我发现了它:https://stackoverflow.com/a/19364698/8207054
问题看起来像这样: 我有这样的文字:
<div>test®</div>
但需要对此进行修改:
<div>test<sup>®</sup></div>
这需要在所有带有®符号的地方进行。如何归档?
答案 0 :(得分:-1)
由于我收到@epascarello的回复,因此可以正常工作:
document.addEventListener("DOMContentLoaded", function(event) {
document.body.innerHTML = document.body.innerHTML.replace(/®/g, '<sup>®</sup>');
});
如果您切换此代码,则从jQuery切换到原始Javascript时,速度会有很大提高。我们测得它比jQuery代码快1至2倍!
请注意:
这将替换每次出现的字符®
,页面上的所有位置。 (甚至是链接,脚本等)。