我正在尝试在我的页面中的每个™,®,©周围添加<sup></sup>
个标签。
我发现了这个问题:CSS superscript registration trademark让我开始了。
该脚本的工作原理是将标记放置在正确的位置,但每个标记周围添加两个<sup></sup>
标记而不是一个。
这是我的JS添加标签:
jQuery("body").html(
jQuery("body").html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>®</sup>').
replace(/™/gi, '<sup>™</sup>').
replace(/™/gi, '<sup>™</sup>').
replace(/©/gi, '<sup>©</sup>').
replace(/©/gi, '<sup>©</sup>')
);
如何确保每个符号只添加一次标记?某种条件可能吗?
答案 0 :(得分:24)
我没有重写整个标记(并删除所有绑定事件),而是采用类似的方式:
$('body :not(script)').contents().filter(function() {
return this.nodeType === 3;
}).replaceWith(function() {
return this.nodeValue.replace(/[™®©]/g, '<sup>$&</sup>');
});
答案 1 :(得分:2)
这适合我。
$("p,h1,h2,h3,h4,li,a").each(function(){
$(this).html($(this).html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>® </sup>'));
});
答案 2 :(得分:2)
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>™</sup>");
});
})(jQuery, Drupal);