内联 jquery 字符串 - 为某些单词添加粗体字体

inline jquery string - add bold font to certain words

本文关键字:添加 单词 字体 jquery 字符串 内联      更新时间:2023-09-26

我有一个jquery的内联字符串,它显示动态文本字符串,如下所示:

$( "#submit_buttonA" ).attr('update-confirm', '{% trans "Are you sure you want to change the language of the website from" %} ' + $('#id_language_code option[value=' + '{{ user.get_profile.language_preference }}' + ']').text() + ' {% trans "to" %} ' + $('#id_language_code option[value=' + newLanguageCode + ']').text() + '{% trans "?" %}');

这给了我以下内容:

您确定要将网站的语言从葡萄牙语(巴西)更改为意大利语 - 意大利语吗?

我现在正在尝试以粗体字体制作语言名称,但我采用的每种方法都会将 字符呈现到屏幕上。

这就是我想要实现的目标:

您确定要将网站的语言从葡萄牙语(巴西)更改为意大利语 - 意大利语吗?

所以我尝试了以下方法:

$( "#submit_buttonA" ).attr('update-confirm', '{% trans "Are you sure you want to change the language of the website from" %}<b> ' + $('#id_language_code option[value=' + '{{ user.get_profile.language_preference }}' + ']').text() + '</b> {% trans "to" %}<b> ' + $('#id_language_code option[value=' + newLanguageCode + ']').text() + '</b>{% trans "?" %}');

这就是我得到的:

是否确实要将网站语言从 português (Brasil) 更改为 Italiano - Italiano

编辑:根据请求添加代码

        $('a[update-confirm]').click(function(ev) {
        var href = $(this).attr('href');
        if (!$('#updateConfirmModal').length) {
            //please wait included in the line of code below.
            $('body').append('<div id="updateConfirmModal" class="modal modal-confirm-max-width" role="dialog" aria-labelledby="updateConfirmLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true"><icon class="icon-remove"></icon></button><h4 class="modal-title" id="updateConfirmLabel">{% trans "Confirm Language Change" %}</h4></div><div class="modal-body"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">{% trans "Cancel" %}</button>&nbsp;&nbsp;<span class="visible-phone"><br /></span><a class="btn-u btn-u-blue" id="updateConfirmOK" onclick="submitForm();showProgressAnimation();">{% trans "Update Language View" %} - {% trans "Language Change" %}</a></div></div>');
        }
        $('#updateConfirmModal').find('.modal-body').text($(this).attr('update-confirm'));
        $('#updateConfirmOK').attr('href', href);
        $('#updateConfirmModal').modal({show:true});
        return false;
    });

更改此行:

$('#updateConfirmModal').find('.modal-body').text($(this).attr('update-confirm'));

到这一行:

 $('#updateConfirmModal').find('.modal-body').html($(this).attr('update-confirm'));

请参考原帖中的评论,关注整个过程

只需将 .text() 更改为 .html()。

.text() 从字面上呈现所有内容,而 .html() 渲染普通 HTML。