簡體   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