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

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

本文关键字:上标 sup 标签 添加 周围 符号 注册商标      更新时间:2023-09-26

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

发现了这个问题:CSS上标注册商标,这有助于我入门。

该脚本的工作原理是将标签放置在正确的位置,但它在每个标签周围添加两个<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>')
);

如何确保每个符号只添加一次标签?也许是某种条件?

与其重写整个标记(并删除所有绑定事件),我会选择这样的东西:

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

演示:http://jsfiddle.net/QTfxC/

这对

我有用。

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

@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-codes.com 上的十六进制,并选择了角色的®十六进制。值是AE,所以这就是我得到解决方案的方式。让我知道这个是否适合你!

这在我的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);