Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

javascript - Adding superscript <sup> tags around all trademark and registered trademark symbols

I am trying to add <sup></sup> tags around every ™, ®, © in my page.

I found this question: CSS superscript registration trademark which helped get me started.

The script works in the sense that the tags are being placed in the proper locations, but it is adding two <sup></sup> tags around each instead of just one.

Here is my JS adding the tags:

jQuery("body").html(
    jQuery("body").html().replace(/&reg;/gi, '<sup>&reg;</sup>').replace(/?/gi, '<sup>&reg;</sup>').
        replace(/&trade;/gi, '<sup>&trade;</sup>').
        replace(/?/gi, '<sup>&trade;</sup>').
        replace(/&copy;/gi, '<sup>&copy;</sup>').
        replace(/?/gi, '<sup>&copy;</sup>')
);

How can I make sure the tags are only added once per symbol? A conditional of some sort maybe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of rewriting the entire markup (and removing all bound events), I'd go for something like that:

$('body :not(script)').contents().filter(function() {
    return this.nodeType === 3;
}).replaceWith(function() {
    return this.nodeValue.replace(/[???]/g, '<sup>$&</sup>');
});

DEMO: http://jsfiddle.net/QTfxC/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...