【发布时间】:2020-07-20 11:26:09
【问题描述】:
我有一个带参数的 javascript 变量,但我不知道如何将它传递到我的 html 代码中。 javascript代码取自https://gist.github.com/EvanHahn/2587465:
var caesarShift = function(str, amount) {
// Wrap the amount
if (amount < 0)
return caesarShift(str, amount + 26);
// Make an output variable
var output = '';
// Go through each character
for (var i = 0; i < str.length; i ++) {
// Get the character we'll be appending
var c = str[i];
// If it's a letter...
if (c.match(/[a-z]/i)) {
// Get its code
var code = str.charCodeAt(i);
// Uppercase letters
if ((code >= 65) && (code <= 90))
c = String.fromCharCode(((code - 65 + amount) % 26) + 65);
// Lowercase letters
else if ((code >= 97) && (code <= 122))
c = String.fromCharCode(((code - 97 + amount) % 26) + 97);
}
// Append
output += c;
}
// All done!
return output;
};
显然我想将它传递给我的 HTML。我做了一些研究,并遇到了以下方法:
<p id="output"></p>
然后
document.getElementById('output').innerHTML = lengthOfName;
但我不知道如何将它们加在一起。如何调用变量?对于字符串,我有一个文本区域输入框,也许还有第二个参数的点击器,金额,但我不知道如何将它们放在 HTML 中。
【问题讨论】:
标签: javascript html encryption caesar-cipher