Simple application of JavaScript nine palace click color change

Posted by victor on Thu, 26 Mar 2020 18:49:45 +0100

Finish the layout of nine palace grid, and change the background color of any grid after clicking it.

First, use the table to complete the nine palace grid frame:

<table>
    <tr >
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr >
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr >
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>

Set the style of the nine palace grid:

    <style>
        table{
            height: 600px;
            width: 600px;
            border-spacing: 0;
        }
        td{
            margin: 0;
            border:1px solid red;
        }

    </style>

Finally, in order to achieve the click effect of each box, add onclick attribute to each td, and reference the object itself through this:

<table>
    <tr >
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
    </tr>
    <tr >
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
    </tr>
    <tr >
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
    </tr>
</table>

I tried to set click event through CSS again, like this:

    <style>
        td{
            onclick:change(this);
        }

    </style>

But it can't be used. At present, it's still doubtful.
Finally, the most important part of JS code is to use Math.random()*256 to generate a random number between 0-256, then use parseInt() to convert the type to an integer, and use ". style.backgroundColor" to set the background color:

    function change(a) {
         var randomNum=parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+","+parseInt(Math.random()*256);
         a.style.backgroundColor="rgb("+randomNum+")";
     }

The complete code is as follows:
table version:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        table{
            height: 600px;
            width: 600px;
            border-spacing: 0;
        }
        td{
            margin: 0;
            border:1px solid red;
        }

    </style>
</head>
<body>
 <!--Use the front-end knowledge to realize nine palace grid layout, click any grid to randomly change the grid background color-->
<table>
    <tr >
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
    </tr>
    <tr >
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
    </tr>
    <tr >
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
        <td onclick="change(this)"></td>
    </tr>
</table>
 <script>
     function change(emle) {
         console.log("1");
         var randomNum=parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+","+parseInt(Math.random()*256);
         console.log(randomNum);
         emle.style.backgroundColor="rgb("+randomNum+")";

     }
 </script>
</body>
</html>

Div version:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        #box{
            margin:0 auto;
            width: 306px;
            height: 306px;
            border: 1px solid red;

        }
        #box div{
            width: 100px;
            height: 100px;
            border: 1px solid #ccc;
            float: left;
        }

    </style>
</head>
<body>
<div id="box">
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
    <div onclick="changeColor(this)"></div>
</div>
<script>
    function changeColor(elem){
        var red = parseInt(Math.random()*256);
        var blue = parseInt(Math.random()*256);
        var green = parseInt(Math.random()*256);
        elem.style.backgroundColor ="rgb("+red+","+blue+","+green+")";
    }
</script>
</body>
</html>

Topics: Attribute