随机字体大小并设置字符的最小值和最大值
Random font size and set a min and max of the character
我这里有一些问题。
a)如何在此代码中添加随机字体大小?
b)如何设置min(3)和max(8)个字符的限制?
Javascript
function randomString(Length) {
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").innerHTML = randomString(Length);
}, 2000);
}
<!DOCTYPE html>
<html>
<body>
<p id="myP">This is a paragraph.</p>
<button type="button" onclick="myFunction()">Set font size</button>
<script>
function myFunction() {
document.getElementById("myP").style.fontSize = Math.floor((Math.random() * 8) + 3)+"px";
}
</script>
</body>
</html>
这应该可以完成你的工作
用于添加随机字体大小
document.getElementById("myP").style.fontSize = Math.floor((Math.random() * 8) + 3)+"px";
您必须实现自己的代码才能使其随机化
如何设置最少(3)和最多(8)个字符的限制?
function randomString(Length)
{
if(Length < 3) Length = 3;
if(Length > 8) Length = 8;
我这里有一些问题。
a)如何在此代码中添加随机字体大小?
b)如何设置min(3)和max(8)个字符的限制?
Javascript
function randomString(Length) {
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").innerHTML = randomString(Length);
}, 2000);
}
<!DOCTYPE html>
<html>
<body>
<p id="myP">This is a paragraph.</p>
<button type="button" onclick="myFunction()">Set font size</button>
<script>
function myFunction() {
document.getElementById("myP").style.fontSize = Math.floor((Math.random() * 8) + 3)+"px";
}
</script>
</body>
</html>
这应该可以完成你的工作
用于添加随机字体大小
document.getElementById("myP").style.fontSize = Math.floor((Math.random() * 8) + 3)+"px";
您必须实现自己的代码才能使其随机化
如何设置最少(3)和最多(8)个字符的限制?
function randomString(Length)
{
if(Length < 3) Length = 3;
if(Length > 8) Length = 8;