用JS取出后台返回数据中用标签样式包裹的内容
·
后台返回的数据是这样的:
`<p><span style="color: rgb(51, 51, 51); font-family: "Open Sans", sans-serif;
font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps:
normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: center; text-
indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -
webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-
style: initial; text-decoration-color: initial; display: inline !important; float: none;"
>餐饮复工需要什么手续和准备工作。</span><br>
</p>`
而我只想要
餐饮复工需要什么手续和准备工作。
封装装一个字符串处理函数供自己以后使用:
const dealString = (str)=>{
let result = "";
let flag = false;
for(let char of str){
if(char===">"){
flag = true;
}
if(char==="<"){
flag = false
}
if(flag&&char!==">"){
result=result+char;
}
}
return result;
}
let str = `<p><span style="color: rgb(51, 51, 51); font-family: "Open Sans", sans-serif; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">餐饮复工需要什么手续和准备工作。</span><br>
</p>`
console.log(dealString(str));//餐饮复工需要什么手续和准备工作。
📦 前端资源合集 | 持续更新
🟢 前端0到1【持续更新】→ https://pan.quark.cn/s/5df55ccff7c4
🔵 前端进阶【持续更新】→ https://pan.quark.cn/s/2dec1c87b3ec
🟣 前端2026最新【持续更新】→ https://pan.quark.cn/s/77c8fa94161c
🔴 AI最新学习资料 → https://pan.baidu.com/s/1P9X2Qk_Fby3rFNVGw_WKow?pwd=46XG 提取码:46XG
觉得有用就点个赞+收藏,关注我持续分享前端干货 💡
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐

所有评论(0)