JS實現(xiàn)HTML標簽轉(zhuǎn)義及反轉(zhuǎn)義
簡單說一下業(yè)務(wù)場景,前臺用戶通過input輸入內(nèi)容,在離開焦點時,將內(nèi)容在div中顯示。
這時遇到一個問題,如果用戶輸入了html標簽,則在div顯示中,標簽被解析。
由于是純前端操作,不涉及后端,因此需要通過js對輸入內(nèi)容進行轉(zhuǎn)義。
這里提供一個非常簡單有效的轉(zhuǎn)義方案,利用了innerHTML和innerText
注:火狐不支持innerText,需要使用 textContent 屬性,而IE早期版本不支持此屬性,為了同時兼容IE及火狐,需要進行判斷操作.
因為innerText(textContent)會獲取純文本內(nèi)容,忽略html節(jié)點標簽,而innerHTML會顯示標簽內(nèi)容,
所以我們先將需轉(zhuǎn)義的內(nèi)容賦值給innerText(textContent),再獲取它的innerHTML屬性,這時獲取到的就是轉(zhuǎn)義后文本內(nèi)容。
代碼如下:
function HTMLEncode(html) { var temp = document.createElement("div"); (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html); var output = temp.innerHTML; temp = null; return output; } var tagText = "<p><b>123&456</b></p>"; console.log(HTMLEncode(tagText));//<p><b>123&456</b></p>
通過測試結(jié)果,可以看到html標簽及&符都被轉(zhuǎn)義后保存。
同理,反轉(zhuǎn)義的方法為先將轉(zhuǎn)義文本賦值給innerHTML,然后通過innerText(textContent)獲取轉(zhuǎn)義前的文本內(nèi)容
function HTMLDecode(text) { var temp = document.createElement("div"); temp.innerHTML = text; var output = temp.innerText || temp.textContent; temp = null; return output; } var tagText = "<p><b>123&456</b></p>"; var encodeText = HTMLEncode(tagText); console.log(encodeText);//<p><b>123&456</b></p> console.log(HTMLDecode(encodeText)); //<p><b>123&456</b></p>
編碼反編碼核心函數(shù)
function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); s = s.replace(/\n/g, "<br/>"); return s; } function html_decode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/'/g, "\'"); s = s.replace(/"/g, "\""); s = s.replace(/<br\/>/g, "\n"); return s; } console.log(html_decode('<div>123</div>')); console.log(html_encode(html_decode('<div>123</div>')));
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>test</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); s = s.replace(/\n/g, "<br/>"); return s; } function html_decode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/'/g, "\'"); s = s.replace(/"/g, "\""); s = s.replace(/<br\/>/g, "\n"); return s; } console.log(html_decode('<div>123</div>')); console.log(html_encode(html_decode('<div>123</div>'))); </script> </head> <body> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- JS及JQuery對Html內(nèi)容編碼,Html轉(zhuǎn)義
- JS轉(zhuǎn)換HTML轉(zhuǎn)義符的方法
- javascript對HTML字符轉(zhuǎn)義與反轉(zhuǎn)義
- 在JavaScript中對HTML進行反轉(zhuǎn)義詳解
- js處理網(wǎng)頁編輯器轉(zhuǎn)義、去除轉(zhuǎn)義、去除HTML標簽的正則
- 對字符串進行HTML編碼和解碼的JavaScript函數(shù)
- Javascript String對象擴展HTML編碼和解碼的方法
- JavaScript中最簡潔的編碼html字符串的方法
- JS Html轉(zhuǎn)義和反轉(zhuǎn)義(html編碼和解碼)的實現(xiàn)與使用方法總結(jié)
相關(guān)文章
js中int和string數(shù)據(jù)類型互相轉(zhuǎn)化實例
在本篇文章里小編給大家分享了關(guān)于js中int和string數(shù)據(jù)類型互相轉(zhuǎn)化實例和代碼,需要的朋友們學(xué)習(xí)下。2019-01-01javascript匿名函數(shù)應(yīng)用示例介紹
匿名函數(shù),顧名思義就是沒有名字,下面有個不錯的示例,大家可以學(xué)習(xí)下2014-03-03nodejs讀取本地中文json文件出現(xiàn)亂碼解決方法
在本篇文章中我們給大家分享了關(guān)于nodejs讀取本地中文json文件出現(xiàn)亂碼的解決方法,需要的朋友們可以學(xué)習(xí)下。2018-10-10微信小程序使用canvas自適應(yīng)屏幕畫海報并保存圖片功能
這篇文章主要介紹了小程序使用canvas自適應(yīng)屏幕畫海報并保存圖片功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07JS如何判斷是否為ie瀏覽器的方法(包括IE10、IE11在內(nèi))
這篇文章主要介紹了JS如何判斷是否為ie瀏覽器的方法(包括IE10、IE11在內(nèi)),需要的朋友可以參考下2015-12-12Bootstrap富文本組件wysiwyg數(shù)據(jù)保存到mysql的方法
這篇文章主要為大家詳細介紹了Bootstrap富文本組件wysiwyg數(shù)據(jù)保存到mysql的方法,感興趣的小伙伴們可以參考一下2016-05-05