我在这里遇到了一些问题。
a)我想设置3-8个字符,但它只显示3个字符。
b)我想有三个不同显示时间的输出。我只能创建不同的ID吗?
任何人都可以提供帮助?这段代码出了什么问题?感谢。
的Javascript
function randomString(Length)
{
if(Length < 3) Length = 3;
if(Length > 8) Length = 8;
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( var i=0; i < Length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function ChangingRandomString(Length)
{
setInterval(function(){document.getElementById("random").style.fontSize = Math.floor((Math.random() * 20) + 10)+"px",
document.getElementById("random").innerHTML = randomString(Length);
},2000);
}
答案 0 :(得分:1)
function randomString(length) {
if (length < 3) length = 3;
if (length > 8) length = 8;
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (var i = 0; i < length; i++) {
text += possible[Math.floor(Math.random() * possible.length)];
}
return text;
}
function ChangingRandomString(length) {
setInterval(() => {
let el = document.getElementById('random');
el.style.fontSize = Math.floor(Math.random() * 20 + 10) + 'px';
el.innerHTML = randomString(length);
}, 2000);
}
ChangingRandomString(length);
<div id="random"></div>
答案 1 :(得分:0)
function randomString(length){
if(length<5) length =5;
if(length>8) length =8;
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(let i=0;i<=length;i++){
text +=possible[Math.floor(Math.random()*possible.length)]
}
return text
}
function string(length){
setInterval(() => {
console.log(randomString(length));
},2000);
}
string(5);