【问题标题】:Random font size and set a min and max of the character随机字体大小并设置字符的最小值和最大值
【发布时间】:2017-08-21 16:00:46
【问题描述】:

我这里有一些问题。

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

【问题讨论】:

    标签: javascript font-size


    【解决方案1】:

    用于添加随机字体大小

    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;
    

    【讨论】:

      【解决方案2】:

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

      这应该可以完成你的工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-04
        • 2017-12-25
        • 1970-01-01
        • 1970-01-01
        • 2019-07-09
        • 2020-05-10
        • 2018-05-30
        相关资源
        最近更新 更多