我正在创建一个网页来计算一个简单的凯撒密码而不使用jquery。我找不到错误,我不知道如何将新字符串返回到文本区域。
HTML:
<input type="button" value="Encrypt value = 1" onclick ="caesarEncipher(shift, text)"/>
的javascript:
function caesarEncipher(shift, plaintext) {
this.shift = shift;
this.plaintext = plaintext;
var ciphertext
for (var i = 0; i < plaintext.length; i++) {
// ASCII value - get numerical representation
// 65 = 'A' 90 = 'Z'
var encode = plaintext.charCodeAt(i);
if (encode >= 65 && encode <= 90)
// Uppercase
ciphertext += String.fromCharCode((encode - 65 + shift) % 26 + 65);
// 97 = 'a' 122 = 'z'
else if (encode >= 97 && encode <= 122)
// Lowercase
ciphertext += String.fromCharCode((encode - 97 + shift) % 26 + 97);
else
ciphertext += input.charAt(i);
}
return document.getElementById = ciphertext; <-- Not sure about this
}
答案 0 :(得分:0)
function encrypt(id, shiftId)
{
var t = document.getElementById(id), out = '';
var shift = parseInt(document.getElementById(shiftId).value);
var txt = t.value, ranges = [[65,90],[97,122]];
for(var i = 0; i < txt.length; i++)
{
var code = txt.charCodeAt(i);
for(var j = 0; j < ranges.length; j++)
{
if (code >= ranges[j][0] && code <= ranges[j][1])
{
code = ((code - ranges[j][0] + shift) %
(ranges[j][1] - ranges[j][0] + 1)) + ranges[j][0];
break;
}
}
out += String.fromCharCode(code);
}
t.value = out;
}
<textarea id='t'></textarea><br><input type='text' id='s' value='1'><br>
<input type='button' onclick='encrypt("t", "s")' value='Go'>