![](https://img.haomeiwen.com/i13133049/42569b956429dd48.png)
HTML特殊符号编码有很多种 unicode、十六进位码(hex code),html 实体编码(entity code),还有我们熟知的html 实体(html entity) ,为了在css content属性中使用,还有对应的 css code。
你可以在HTML标签中直接插入十六进位码(hex code),html 实体编码(entity code)或者 html 实体(html entity)。在css content属性中使用则应该使用对应的 css code。
需要特别注意的是:根据字体不同,部分符号显示有所不同。
在项目应用中,有些时候需要将 html 和 html code进行转换,这里提供互转的两个方法:
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;
}
使用DOM 的 innerHTML 和 textContent 也可以实现转换,方法是动态创建一个容器标签元素,如 DIV,将要转换的字符串设置为这个元素的 innerText,然后返回这个元素的 innerHTML,即得到经过 HTML 编码转换的字符串。
JavaScript 代码:
function html_encode(html)
{
return document.createElement('div')
.appendChild(document.createTextNode(html))
.parentNode.innerHTML;
}
function html_decode(html)
{
var a = document.createElement('div');
a.innerHTML = html;
return a.textContent;
}
HTML Arrows 整理了大部分网站常用到的特殊符号,供大家参考使用:https://www.toptal.com/designers/htmlarrows/
以下也有整理好的图片,方便大家保存,运用。喜欢的点个赞
如果你也是一个前端党,无论是在学习前端开发,还是已经工作的,这里推荐一下我们的前端学习交流群:731771211,这里是把梦想照亮的地方,同为了生活而拼搏奋斗,大家互相帮助。新手加入即可获得经过整理的最前沿的前端技术资料,不定时更新技术,与企业需求同步。好友都在里面交流,每天都会有大牛定时讲解前端技术!知识改变命运
点击:加入
![](https://img.haomeiwen.com/i13133049/c460e46f5d64bd47.png)
![](https://img.haomeiwen.com/i13133049/33891b68d993ad13.png)
![](https://img.haomeiwen.com/i13133049/168a43ad0210def6.png)
![](https://img.haomeiwen.com/i13133049/f31f541e2e9a014d.png)
![](https://img.haomeiwen.com/i13133049/6419afc723294870.png)
![](https://img.haomeiwen.com/i13133049/638866e2b094cb9a.png)
![](https://img.haomeiwen.com/i13133049/4690d219eaed82a2.png)
![](https://img.haomeiwen.com/i13133049/3c94d5b7b8933f84.png)
网友评论