[英]jQuery escape certain html entities only
我有一個小檔案系統,其中有提及
我的帖子表中的“內容”值存儲為:
<a href="/profile/Alice">Alice</a> Alice, you there? <strong>lol</strong>
但是,最終結果是一切都顯示為HTML(我使用json和jquery顯示它們)。 如何使它只顯示HTML(對於我的提及鏈接),而不顯示所有用戶指定的? (即<strong>)
我希望帖子在超級鏈接中顯示提及,但同時,我不希望所有用戶輸入的html都顯示為html。
我使用jQuery append來顯示我的帖子。
謝謝!
$str = '<a href="/profile/Alice">Alice</a> Alice, you there? <strong>lol</strong>';
$str = html_entity_decode($str);
echo $str;
exit;
如果我了解您想要什么,我認為這可以做到。 find("*")
僅匹配HTML標記,因此標記外的文本節點被過濾掉。
var content = '<a href="/profile/Alice">Alice</a> Alice, you there? <strong>lol</strong>'; var wrapped = $("<div>", { html: content }); $("#output").append(wrapped.find("*"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="output"></div>
我首先將其包裝在DIV
,因為.find()
僅搜索子級,而不搜索頂級節點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.