繁体   English   中英

在javascript中转义/取消转义html?

[英]Escaping/Unescaping html in javascript?

关于从js转义/取消转义html,有许多关于stackoverflow的回答问题。 但是我发现很难理解,因为它不适合我的情况。 我有一个包含以下字符串的变量“ message”:

message="<a href="www.google.com">Google</a>"

我正在使用“ this.message”在js文件中显示此消息。 我可以看到整个字符串,而不是看到超链接的“ Google”:

<a href="www.google.com">Google</a>

如果检查元素,我可以看到该字符串已被转换为:

&lt;a href="www.google.com"&gt;Google;&lt;&gt

如何获得Google的超链接? 我是否必须逃脱/逃脱? 怎么样?

显示消息的代码:

$.each(mdn.notifications || [], function() {
            $('<div class="notification">' + this.message + '</div>')[insertLocation.method](insertLocation.selector);
        });

在django模板中添加html格式,如下所示:

{% if messages %}notifications: [
                {% for message in messages %}{% if 'wiki_redirect' not in message.tags or waffle.flag('redirect_messages') %}{message: "<div class='my-message'>{{ message }}</div>", tags: "{{ message.tags }}", level: "{{ message|level_tag }}"}{% if not loop.last %},{% endif %}{% endif %}
                {% endfor %}
            ],
            {% else %}
            notifications: [],
            {% endif %}

或者您可以在客户端javascript中添加html格式,如下所示:

$.each(mdn.notifications || [], function() {
            // Make it so
            $('<div class="notification ' + this.level + ' ' + this.tags + '" data-level="' + this.level + '"><div class="my-message">' + this.message + '</div></div>')[insertLocation.method](insertLocation.selector);
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM