vue中處理后臺(tái)返回的 html 特殊標(biāo)簽(‘\< p style=“xxx“ \>‘)或(\<p>)的三種情況

2021-4-27    前端達(dá)人

在平時(shí)獲取后臺(tái)數(shù)據(jù)渲染頁(yè)面的時(shí)候可能會(huì)出現(xiàn)后臺(tái)返回的數(shù)據(jù)是帶有 html 特殊標(biāo)簽的
需求是附帶的樣式也不要, 意思就是直接刪掉那些內(nèi)容
但是在網(wǎng)上找挺久都沒(méi)有找到現(xiàn)成的方法 最后是自己找了兩個(gè)方法拼接出來(lái)的 所以在這里總結(jié)一下 方便以后直接 cv

// 返回?cái)?shù)據(jù):  ret : { list: { "introduct": '&lt;p style="color: lightcoral;"&gt;就&nbsp;當(dāng)文字就是內(nèi)容吧。&lt;/p&gt;', } } // 或者 ret : { list: { "introduct": '<span style="color: skyblue">就當(dāng)文字就是內(nèi)容吧。&nbsp;</p>', } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

返回?cái)?shù)據(jù)帶有html特殊字符(’&nbsp;/&le;p&gt;’) -> 轉(zhuǎn)成 html標(biāo)簽(<p>)

// 返回?cái)?shù)據(jù)帶有html特殊字符的話直接用 v-html 在頁(yè)面上顯示的是 html標(biāo)簽 <body> <div id="app"> <div class="fd"> {{message}} // 如果需要數(shù)據(jù)中的樣式的可以直接 v-html 指令渲染這個(gè)字段就能渲染出來(lái)了 <div class="box" v-html="'v-html: ' + message"></div> <button class="btn" @click="click1">dianwo</button> </div> </div> </body> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el: '#app', data: { message: '&lt;p style="color: lightcoral;"&gt;就&nbsp;當(dāng)文字就是內(nèi)容吧。&lt;/p&gt;' }, methods: { click1() { this.message = this.escapeHtml(this.message) }, // 處理方法 escapeHtml(str) { var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' }; return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) { return arrEntities[t]; }); }, } }) </script> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在這里插入圖片描述

上面這個(gè)方法來(lái)自:https://blog.csdn.net/weixin_49186680/article/details/108746341

返回?cái)?shù)據(jù)帶有 html標(biāo)簽(<span>) -> 把 html標(biāo)簽 去掉

// 如果不想要后臺(tái)返回在數(shù)據(jù)終的樣式的話可以這樣處理 <body> <div id="app"> <div class="fd"> {{message}} <div class="box" v-html="'v-html: ' + message"></div> <button class="btn" @click="click1">dianwo</button> </div> </div> </body> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el: '#app', data: { message: '<span style="color: skyblue">就當(dāng)文字就是內(nèi)容吧。&nbsp;</span>' }, methods: { click1() { this.message = this.delHtmlTag(this.message) }, // 處理方法 delHtmlTag(str) { return str.replace(/<[^>]+>/g, '').replace(/&nbsp;/ig, "") } } }) </script> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

在這里插入圖片描述

上面的方法來(lái)自:https://blog.csdn.net/weixin_44565191/article/details/109716908

返回?cái)?shù)據(jù)帶有html特殊字符(’&nbsp;/&le;p&gt;’) -> 去掉特殊字符

// 如果返回的帶有 html特殊字符 都不要 結(jié)合上面兩個(gè)方法 改吧改吧 就能瞞住要求了 <body> <div id="app"> <div class="fd"> {{message}} <div class="box" v-html="'v-html: ' + message"></div> <button class="btn" @click="click1">dianwo</button> </div> </div> </body> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el: '#app', data: { message: '<span style="color: skyblue">就當(dāng)文字就是&nbsp;內(nèi)&nbsp;容吧</span>' }, methods: { click1() { this.message = this.escapeHtml(this.message) }, // 終極 處理方法 escapeHtml(str) { var arrEntities = { 'lt': '<', 'gt': '>', 'amp': '&', 'quot': '"' }; let htmlTag = str.replace(/&(lt|gt|amp|quot);/ig, function(all, t) { return arrEntities[t]; }); // console.log(htmlTag); return htmlTag.replace(/<[^>]+>/g, '').replace(/&nbsp;/ig, "") }, } }) </script> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在這里插入圖片描述

感謝一下引用的這些大佬的內(nèi)容
還有附上 樣式 要想親自試試效果的我把樣式放這里

.fd { margin: 100px auto; display: flex; flex-direction: column; justify-content: center; align-items: center; } .box { margin: 30px 0; display: flex; align-items: center; } .btn { width: 100px; }





    

轉(zhuǎn)自:csdn 作者小王幾pl


藍(lán)藍(lán)設(shè)計(jì)www.bouu.cn )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國(guó)內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 平面設(shè)計(jì)服務(wù)


分享本文至:

日歷

鏈接

個(gè)人資料

存檔