divをクリックして色をランダムに変更します

1 <!DOCTYPE html>
 2 <html lang = "en">
 3 <head>
 4    <meta charset = "UTF-8">
 5    <meta name = "viewport" content = "width = device-width、initial-scale = 1.0 ">
 6    <title>ドキュメント</ title>
 7    <style>
 8      #box {
 9        width:200px;
10        高さ:200px;
11        背景:赤;
12      }
 13    </ style>
 14 </ head>
 15 <body>
 16    <div id = "box"> </ div>
     box = document.getElementById( 'box' 19      box.onclick = function (){
 20        // 生成一个随机颜色
21        var str = '0123456789abcdef'
 22        var color = '#'
 23        forvar i = 0; i < 6; i ++ ){
 24          var index = Math.floor(Math.random()* str.length)
 25          color + = str [index]
 26        }
 27        console.log(color)
29        box.style.background = color
 30      }
 31    </ script>
32 </ body>
 33 </ html>

 

おすすめ

転載: www.cnblogs.com/strongerPian/p/12716477.html