<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
label{display: block;margin: 20px;}
#box{ 260px; height: 50px; border: solid 1px black; margin-left: 20px;}
</style>
<script type="text/javascript">
//某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换,请编写一个函数,传入原文,输出密文。
function $(id){return document.getElementById(id);}
// 用$代替document.getElementById减少代码重复量
window.onload = function ()
{
oNumber = $('number');
oCheck = $('check');
oBox = $('box');
oCheck.onclick = function()
{
nNumber = Number(oNumber.value);
nThousand = parseInt(nNumber / 1000);
nHundred = parseInt((nNumber - nThousand * 1000) / 100);
nTen = parseInt((nNumber - nThousand * 1000 - nHundred * 100) / 10);
nUnit = parseInt(nNumber%10);
//我将4个数分为个,十,百,千位来看待并通过取余和相除得到
console.log('千'+nThousand,'百'+nHundred,'十'+nTen,'个'+nUnit);
nThousand = ((nThousand + 5)%10);
nHundred = ((nHundred + 5)%10);
nTen = ((nTen + 5)%10);
nUnit = ((nUnit + 5)%10);
console.log('千'+nThousand,'百'+nHundred,'十'+nTen,'个'+nUnit);
oBox.innerHTML = nUnit + ' ' + nTen + ' ' + nHundred + ' ' + nThousand ;
//将千和个的位子换一下数出即可,百和十同理
}
}
</script>
</head>
<body>
<label>请输入密码:<input type="text" id="number" /></label>
<label><input type="button" id="check" value="查看" /></label>
<div id="box"></div>
</body>
</html>
效果图 ↓