[英]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.