尝试在文档就绪中生成随机背景颜色

Trying to generate random background color within document ready

我试图在每次加载和每次按下 "New Quote" 按钮时将我的网络应用程序的背景设置为随机颜色。我用来生成随机背景颜色的函数是 random_bg_color().

我在$(document).ready(function(){})内调用过它。我正在使用 codepen.io 创建此 Web 应用程序。我的网络应用程序的 link 是 here。目前,页面仅加载 "white" 作为背景色。感谢任何有关如何解决此问题的反馈!

    <!DOCTYPE html>
    <html lang="en" >
      <head>
        <meta charset="UTF-8">
        <link rel="shortcut icon" type="image/x-icon" href="https://static.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" />
        <link rel="mask-icon" type="" href="https://static.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" />
        <title>CodePen - Random Quote Getter</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css'>
        <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css'>

        <style>
          #new-quote-btn {

          }
        </style>
      </head>

      <body translate="no" >
        <div class='container-fluid'>
          <button id='new-quote-btn'>New Quote</button>
        </div>

        <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/fontawesome-iconpicker/3.0.0/js/fontawesome-iconpicker.js'></script>
        <script>
          $(document).ready(function() {
            random_bg_color();
            document.getElementById("#new-quote-btn").addEventListener("click", random_bg_color);
          });

          function random_bg_color() {
            var x = Math.floor(Math.random() * 256);
            var y = Math.floor(Math.random() * 256);
            var z = Math.floor(Math.random() * 256);
            var bgColor = "rgb(" + x + "," + y + "," + z + ")";
            console.log(bgColor);
            document.body.style.background = bgColor;
          }
        </script>
      </body>
    </html>

如前所述,您需要将 link 添加到 JQuery,并且您的 getElementById 选择器不正确,因此不会添加任何事件侦听器。删除 # 它将起作用 -

document.getElementById("new-quote-btn").addEventListener("click", random_bg_color);

由于您使用的是 JQuery,因此您可以使用此代码附加事件侦听器 -

$("#new-quote-btn").on("click", function() {
    random_bg_color();
});

在您的 <head> 标签中添加 jQuery CDN。请参阅下面的第 10 行。添加 jQuery.

后,我可以看到它在你的代码笔中工作
<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" type="image/x-icon" href="https://static.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" />
    <link rel="mask-icon" type="" href="https://static.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" />
    <title>CodePen - Random Quote Getter</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css'>
    <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css'>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <style>
      #new-quote-btn {

      }
   </style>
</head>

然后在您的 javascript 中更新您的代码,如下所示

$(document).ready(function() {
    random_bg_color();
    $("#new-quote-btn").on("click", function() {
        random_bg_color();
    });
});

function random_bg_color() {
  var x = Math.floor(Math.random() * 256);
  var y = Math.floor(Math.random() * 256);
  var z = Math.floor(Math.random() * 256);
  var bgColor = "rgb(" + x + "," + y + "," + z + ")";
    console.log(bgColor);
  document.body.style.background = bgColor;
}