Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加上标<;sup>;所有商标和注册商标符号周围的标签_Javascript_Jquery_Symbols_Superscript - Fatal编程技术网

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

Javascript 添加上标<;sup>;所有商标和注册商标符号周围的标签,javascript,jquery,symbols,superscript,Javascript,Jquery,Symbols,Superscript,我正在尝试在每个页面周围添加标记™, ®, © 在我的页面中 我发现了这个问题:它帮助我开始了 该脚本的工作原理是将标记放置在正确的位置,但它在每个标记周围添加了两个标记,而不是一个 下面是添加标签的my JS: jQuery("body").html( jQuery("body").html().replace(/&reg;/gi, '<sup>&reg;</sup>').replace(/®/gi, '<sup>&reg;&

我正在尝试在每个页面周围添加
标记™, ®, © 在我的页面中

我发现了这个问题:它帮助我开始了

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

下面是添加标签的my 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>')
);
jQuery(“body”).html(
jQuery(“body”).html().replace(/®;/gi,“®;”).replace(/®/gi,“®;”)。
替换(/&trade;/gi,“&trade;”)。
替换(/™/gi,“&trade;”)。
替换(/©;/gi,“©;”)。
替换(/)/gi“©;”)
);

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

我不想重写整个标记(并删除所有绑定事件),而是选择这样的方式:

$('body :not(script)').contents().filter(function() {
    return this.nodeType === 3;
}).replaceWith(function() {
    return this.nodeValue.replace(/[™®©]/g, '<sup>$&</sup>');
});
$('body:not(script)').contents().filter(function()){
返回this.nodeType==3;
}).replaceWith(函数(){
返回this.nodeValue.replace(/[™®©]/g、 "元及",;
});
演示:

这对我很有用

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

@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>');
});
var regexp=/[\xAE]/;
$('body:not(script,sup)').contents().filter(function()){
返回this.nodeType===3&(regexp.test(this.nodeValue));
}).replaceWith(函数(){
返回这个.nodeValue.replace(regexp,$&');
});

此方法使用,而不是使用字符代码。我在上查找了十六进制并选择了®字符的字符十六进制。值为
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);
(函数($,Drupal){
var regexp=/[\u2122]/;
$('body:not(script,sup)').contents().filter(function()){
返回this.nodeType===3&(regexp.test(this.nodeValue));
}).replaceWith(函数(){
返回此.nodeValue.replace(“\u2122”,“&trade;”);
});
})(jQuery,Drupal);

如果只替换
(不是
&trade;
)。反过来呢?我怀疑
jQuery(“body”).html()
正在进行一些隐式转换。这是我最初认为的问题,但删除它们并不会改变任何事情。为什么不手动更改源代码?替换body元素的html非常粗糙且容易出错。@TyBailey OK,那么如果只调用一次
replace
,而不是将多个元素链接在一起,会发生什么呢如预期的那样工作?顺便说一句,在包含大量内容的页面上执行此操作将非常缓慢。@undefined是正确的-在源代码处更改它!这非常有效!谢谢。我将尽快接受答案。嘿,Vision,你能帮我修改此操作以检查符号周围是否已经有
标记吗?如果有,那么不要通过脚本添加它,如果不是,那么就添加它?@TyBailey当然。只需添加
标记到过滤器,即
:not(script,sup)
。演示:。包装™ 实际上不需要in,因为它将始终处于较高的位置。在
中包括
sup
:如果您的内容来自混合来源或由多人输入,则排除非常重要,这一点很好。