2017-08-21 167 views
0

我在这里有几个问题。随机字体大小并设置字符的最小和最大值

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); 
} 

回答

0

添加随机丰尺寸

document.getElementById("myP").style.fontSize = Math.floor((Math.random() * 8) + 3)+"px"; 

你必须实现你自己的代码来随机化它

如何设置min(3)和max(8)字符的限制?

function randomString(Length) 
{ 
    if(Length < 3) Length = 3; 
    if(Length > 8) Length = 8; 
0

<!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>

这应该做你的工作