微信扫码登录 ×
js在线unicode编码与解码特效

js在线unicode编码与解码特效

收藏
js在线unicode编码与解码特效
原生js制作可以将中文字符串转换成unicode字符,也可以将unicode字符转换成中文。

使用方法:

1、head引入css文件

<style>
div,
body,
html {
	margin: 0;
	padding: 0;
	user-select: none;
}

.unicode-input,
.unicode-output {
	width: calc(50% - 24px);
	margin: 10px;
	height: 200px;
	padding: 15px 16px;
	border-radius: 15px;
	border: 1px solid #ededed;
	outline: none;
	box-sizing: border-box;
	word-break: break-all;
	overflow-x: hidden;
	overflow-y: auto;
	display: inline-block;
	user-select: text;
}

.unicode-input:focus {
	border: 1px solid #57a5ff;
	box-shadow: 0 0 -1px 2px #2137f8;
}

.unicode-input:empty:before,
.unicode-output:empty:before {
	color: #d4d4d4;
	content: attr(placeholder)
}

@media screen and (max-width:750px) {

	.unicode-input,
	.unicode-output {
		width: calc(100% - 20px);
	}
}

.set-unicode-btn,
.get-unicode-btn {
	display: inline-block;
	max-width: 150px;
	height: 35px;
	border-radius: 3px;
	padding: 4px 8px;
	background-color: #4d4d4d;
	margin: 10px;
	text-align: center;
	line-height: 35px;
	font-size: 18px;
	color: #ffffff;
	cursor: pointer;
	transition: background-color .4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.set-unicode-btn:hover,
.set-unicode-btn:active,
.get-unicode-btn:hover,
.get-unicode-btn:active {
	background-color: #2391df;
}

::-webkit-scrollbar {
	width: 0;
}
</style>

2、body引入HTML代码

<div class="unicode-input" id="unicodeInput" contenteditable="true" placeholder="请输入需要编码的中文字符"></div>
<div class="unicode-output" id="unicodeOutput" tabindex="1" placeholder="编码结果"></div>
<div class="set-unicode-btn" id="setUnicode">编码</div>
<div class="get-unicode-btn" id="getUnicode">解码</div>

<!--js部分-->
<script type="text/javascript">
    function $(e) {
        return document.getElementById(e);
    }
    $('setUnicode').onclick = function () {
        setUnicode();
    }
    $('getUnicode').onclick = function () {
        getUnicode();
    }
    function setUnicode() {
        var in_val = $('unicodeInput').textContent;
        var in_str = '';
        if (in_val) {
            for (var i = 0, len = in_val.length; i < len; i++) {
                in_str += '\\u' + in_val[i].charCodeAt(0).toString(16);
            }
            $('unicodeOutput').textContent = in_str;
        }
    }
    function getUnicode() {
        var out_val = $('unicodeInput').textContent;
        if (out_val && out_val.indexOf('\\u') !== -1) {
            var valArr = out_val.split('\\u'), result = '';
            for (var j = 0, length = valArr.length; j < length; j++) {
                result += String.fromCharCode(parseInt(valArr[j], 16));
            }
            //如果不截取,则会出现空白字符,如何也消除不了
            $('unicodeOutput').textContent = result.slice(1);
        } else {
            alert('不是unicode字符,无需解码!')
        }
    }
</script>

使用声明

1. 本站所有素材(未指定商用),仅限学习交流。
2. 会员在本站下载的原创商用和VIP素材后,只拥有使用权,著作权归原作者及17素材网所有。
3. 原创商用和VIP素材,未经合法授权,请勿用于商业用途,会员不得以任何形式发布、传播、复制、转售该素材,否则一律封号处理。
4. 本平台织梦模板仅展示和个人非盈利用途,织梦系统商业用途请预先授权。

x
×
×

注册

QQ注册 立即下载 微信注册 立即下载

签到成功!

已连续签到1天,连续签到3天可获得50积分

知道了