在所有商标和注册商标符号周围添加上标<sup>标签</sup>

时间:2013-10-14 16:14:54

标签: javascript jquery symbols superscript

我正在尝试在我的页面中的每个™,®,©周围添加<sup></sup>个标签。

我发现了这个问题:CSS superscript registration trademark让我开始了。

该脚本的工作原理是将标记放置在正确的位置,但每个标记周围添加两个<sup></sup>标记而不是一个。

这是我的JS添加标签:

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

如何确保每个符号只添加一次标记?某种条件可能吗?

4 个答案:

答案 0 :(得分:24)

我没有重写整个标记(并删除所有绑定事件),而是采用类似的方式:

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

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

答案 1 :(得分:2)

这适合我。

$("p,h1,h2,h3,h4,li,a").each(function(){
    $(this).html($(this).html().replace(/&reg;/gi, '<sup>&reg;</sup>').replace(/®/gi, '<sup>&reg;   </sup>'));
});

答案 2 :(得分:2)

尽管面对正确的方向,@ VisioN的答案对我来说效果不佳。我稍微调整了一下:

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

此方法使用character hex,而不是使用字符代码。我在character-codes.com上查找了十六进制并选择了®字符的字符十六进制。值为AE,这就是我的解决方案。如果这个适合你,请告诉我!

答案 3 :(得分:0)

这在我的Drupal 8网站上为我工作:

(function ($, Drupal) {
var regexp = /[\u2122]/;
$('body :not(script,sup)').contents().filter(function() {
    return this.nodeType === 3 && (regexp.test(this.nodeValue));
}).replaceWith(function() {
    return this.nodeValue.replace("\u2122", "<sup>&trade;</sup>");
});
})(jQuery, Drupal);
相关问题