Blogger Information
Blog 56
fans 7
comment 11
visits 222473
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS实现HTML标签转义及反转义
樂成的开发笔记
Original
1466 people have browsed it

实例

<!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>

运行实例 »

点击 "运行实例" 按钮查看在线实例


文章参考CSDN

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post