簡體   English   中英

jQuery僅轉義某些html實體

[英]jQuery escape certain html entities only

我有一個小檔案系統,其中有提及

我的帖子表中的“內容”值存儲為:

<a href="/profile/Alice">Alice</a> Alice, you there? &lt;strong&gt;lol&lt;/strong&gt;
  • 我的系統生成了href個人資料鏈接(我將所有提及都替換為指向個人資料的html鏈接)
  • <strong>大聲笑</ strong>是我不希望顯示的用戶定義的HTML(我在所有用戶帖子上都使用htmlentities(),因此他們的html輸入變成了這樣)

但是,最終結果是一切都顯示為HTML(我使用json和jquery顯示它們)。 如何使它只顯示HTML(對於我的提及鏈接),而不顯示所有用戶指定的? (即<strong>)

我希望帖子在超級鏈接中顯示提及,但同時,我不希望所有用戶輸入的html都顯示為html。

我使用jQuery append來顯示我的帖子。

謝謝!

$str = '<a href="/profile/Alice">Alice</a> Alice, you there? &lt;strong&gt;lol&lt;/strong&gt;';
$str = html_entity_decode($str);
echo $str;
exit;

如果我了解您想要什么,我認為這可以做到。 find("*")僅匹配HTML標記,因此標記外的文本節點被過濾掉。

 var content = '<a href="/profile/Alice">Alice</a> Alice, you there? &lt;strong&gt;lol&lt;/strong&gt;'; 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM