为什么 ® 在与 jquery 一起使用时不起作用

why is ® not working when used with jquery

我有一个问题我想把注册商标符号附加到一个按钮上,我希望用 jquery 来完成,因为有很多很多按钮在我使用它时需要这个符号html 例如(下图)它起作用了。

<input type="button" class="button_a" value="Value &reg;" />

但是当我如下使用 jquery 时,它 returns 作为纯文本:

$('.button_a').each(function(){
    $(this).attr("value",$(this).attr("value")+" &reg;");
});

我不想浪费我的时间和 space 将 &reg; 放入所有按钮的值中,所以我希望它在 jquery 中完成,但我在做什么不正确。

我知道这是一个缓慢的问题,但请有人帮我解决这个问题

Javascript也可以使用。

更多信息请评论和询问

谢谢....

您需要通过 html() 处理内容,因为 html 实体是由 html() 函数操作的,请尝试此代码。

$('.button_a').each(function(){
    $(this).val($("<p/>").html($(this).val()+" &reg;").html());
});

因为,虽然您正在操作的 DOM 是从 HTML 文档初始化的,但您现在处理的是 DOM 而不是 HTML。您正在将值 写为文本 而不是 HTML.

您需要使用文字字符 ("®") 或 JavaScript 转义序列 ("\u00AE")。

&reg; 呈现为文本字符串,但您可以使用 unicode \u00AE

$('.button_a').each(function(){
    this.value = this.value +" \u00AE";
});

http://jsfiddle.net/fbuohvm1/

®:

$('.button_a').each(function(){
    this.value = this.value +" ®";
});

http://jsfiddle.net/fbuohvm1/1/

在设置文本框的值之前,您必须先encode它。

$('.button_a').each(function () {

    $(this).val("value " + $("<div/>").html('&reg;').text());
});

$("<div/>").html('&reg;').text() 将 return 编码值。

演示:https://jsfiddle.net/tusharj/t2gwptys/

使用隐藏 div.

$('.button_a').each(function(){
    var decoded = $('<div/>').html('&reg;').text();
    $(this).val($(this).val() + ' ' + decoded);
});

Sample

在你的情况下,纯 js 更直接:

$('.button_a').each(function(){
    this.value += " &reg;";
});

-- 编辑 -- 不幸的是,这仍然是逃脱的,实际上你需要

    this.value += " \u00AE";