溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用javascript怎么對HTML字符進行轉義

發布時間:2021-02-24 17:35:55 來源:億速云 閱讀:518 作者:Leah 欄目:web開發

這篇文章給大家介紹使用javascript怎么對HTML字符進行轉義,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

1.背景:在項目中,經常遇到一些字符需要進行轉義后才能顯示到界面上,如“&”,在界面中顯示的是“&”,在html中書寫“&”,顯示在界面的中的依然是“&”。

這時候,就需要進行轉義

 使用javascript怎么對HTML字符進行轉義

2.解決方案

<script>

var HtmlUtil = {

 /*1.用瀏覽器內部轉換器實現html轉碼*/

 htmlEncode:function (html){

 //1.首先動態創建一個容器標簽元素,如DIV

 var temp = document.createElement ("div");

 //2.然后將要轉換的字符串設置為這個元素的innerText(ie支持)或者textContent(火狐,google支持)

 (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);

 //3.最后返回這個元素的innerHTML,即得到經過HTML編碼轉換的字符串了

 var output = temp.innerHTML;

 temp = null;

 return output;

 },

 /*2.用瀏覽器內部轉換器實現html解碼*/

 htmlDecode:function (text){

 //1.首先動態創建一個容器標簽元素,如DIV

 var temp = document.createElement("div");

 //2.然后將要轉換的字符串設置為這個元素的innerHTML(ie,火狐,google都支持)

 temp.innerHTML = text;

 //3.最后返回這個元素的innerText(ie支持)或者textContent(火狐,google支持),即得到經過HTML解碼的字符串了。

 var output = temp.innerText || temp.textContent;

 temp = null;

 return output;

 },

 /*3.用正則表達式實現html轉碼*/

 htmlEncodeByRegExp:function (str){ 

 var s = "";

 if(str.length == 0) return "";

 s = str.replace(/&/g,"&amp;");

 s = s.replace(/</g,"&lt;");

 s = s.replace(/>/g,"&gt;");

 s = s.replace(/ /g,"&nbsp;");

 s = s.replace(/\'/g,"'");

 s = s.replace(/\"/g,""");

 return s; 

 },

 /*4.用正則表達式實現html解碼*/

 htmlDecodeByRegExp:function (str){ 

 var s = "";

 if(str.length == 0) return "";

 s = str.replace(/&amp;/g,"&");

 s = s.replace(/&lt;/g,"<");

 s = s.replace(/&gt;/g,">");

 s = s.replace(/&nbsp;/g," ");

 s = s.replace(/'/g,"\'");

 s = s.replace(/"/g,"\"");

 return s; 

 }

 };

</script>

使用方法:HtmlUtil.htmlDecodeByRegExp("&amp;")

PS:使用正則

使用正則轉碼:

var value = document.getElementById('input').value.trim();
 //對用戶輸入進行轉義
 value = value.replace(/&/g,"&amp;");
 value = value.replace(/</g,"&lt;");
 value = value.replace(/>/g,"&gt;");
 value = value.replace(/ /g,"&nbsp;");
 value = value.replace(/"/g,'&quot;');

使用正則解碼:

var value = e.target.innerText;
 // value = decodeURIComponent(value);
 value = value.replace(/&amp;/g,"&");
 value = value.replace(/&lt;/g,"<");
 value = value.replace(/&gt;/g,">");
 value = value.replace(/&nbsp;/g," ");
 value = value.replace(/&quot/g,"'");

下面是其他網友的補充

在編寫html時,經常需要轉義,才能正常顯示在頁面上。

并且,還可以防止xss。

解決方案:

一, 使用正則:

使用正則轉碼:

var value = document.getElementById('input').value.trim();
 //對用戶輸入進行轉義
 value = value.replace(/&/g,"&amp;");
 value = value.replace(/</g,"&lt;");
 value = value.replace(/>/g,"&gt;");
 value = value.replace(/ /g,"&nbsp;");
 value = value.replace(/"/g,'&quot;');

使用正則解碼:

var value = e.target.innerText;
// value = decodeURIComponent(value);
 value = value.replace(/&amp;/g,"&");
 value = value.replace(/&lt;/g,"<");
 value = value.replace(/&gt;/g,">");
 value = value.replace(/&nbsp;/g," ");
 value = value.replace(/&quot/g,"'");

關于使用javascript怎么對HTML字符進行轉義就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女