[英]Adding superscript <sup> tags around all trademark and registered trademark symbols
我想在我的页面中的每个™,®,©周围添加<sup></sup>
标签。
我发现了这个问题: CSS上标注册商标让我开始了。
该脚本的工作原理是将标记放置在适当的位置,但它在每个标记周围添加两个<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>')
);
如何确保每个符号只添加一次标签? 某种条件可能吗?
我没有重写整个标记(并删除所有绑定事件),而是采取类似的方式:
$('body :not(script)').contents().filter(function() {
return this.nodeType === 3;
}).replaceWith(function() {
return this.nodeValue.replace(/[™®©]/g, '<sup>$&</sup>');
});
演示: http : //jsfiddle.net/QTfxC/
尽管面对正确的方向,@ 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
,这就是我的解决方案。 如果这个适合你,请告诉我!
这适合我。
$("p,h1,h2,h3,h4,li,a").each(function(){
$(this).html($(this).html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>® </sup>'));
});
这在我的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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.