Home > Web Front-end > JS Tutorial > JavaScript example sharing about color change

JavaScript example sharing about color change

黄舟
Release: 2017-08-09 11:00:31
Original
1819 people have browsed it

Now we use javascript to make a small example of changing color. The specific steps are as follows.


1. The first step:

First create an HTML file:

JavaScript example sharing about color change

2. The second step:

Write script code:

<body>
  <div id="a1" onclick ="color();"></div>
    
</body>
<script>
    function color() {
        var div = document.getElementById("a1");
        div.id = "a2";
    }
</script>
Copy after login

JavaScript example sharing about color change

3. Step 3:

Style:

<style>
    div
    {
        width:660px;
        height:660px;
    }
    #a1
    {
        background :green;
    }
    #a2
    {
        background :red;
        
    }
 
 </style>
Copy after login

JavaScript example sharing about color change

4. Step 4:

Running result:

JavaScript example sharing about color change

5. Step 5:

Click to change color:

JavaScript example sharing about color change

Friends, you can try it yourself now~

The above is the detailed content of JavaScript example sharing about color change. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template