尝试在文档准备中生成随机背景颜色

时间:2018-05-09 08:07:04

标签: javascript jquery css html5 codepen

我试图在每次加载时将我的网络应用的背景设置为随机颜色,并且每次按下“新报价”按钮。我用来生成随机背景颜色的函数是random_bg_color()

我在$(document).ready(function(){})内拨打了它。我正在使用codepen.io创建这个Web应用程序。我的网络应用程序链接是here。目前,页面仅加载“白色”作为背景颜色。任何有关如何解决此问题的反馈表示赞赏!

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

2 个答案:

答案 0 :(得分:2)

如上所述,您需要添加一个指向JQuery的链接,并且您的getElementById具有错误的选择器,因此不会添加任何事件侦听器。删除#,它将起作用 -

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

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

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

答案 1 :(得分:0)

<head>标记中添加jQuery CDN。见下面第10行。在添加jQuery之后,我可以看到它在您的codepen中工作。

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