JavaScript点击来回切换两个div

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

<style>
    .test1{
        width:100px;
        height: 100px;
        background-color: #5C874A;
    }
    .test2{
        width:100px;
        height: 100px;
        background-color: #66D9B1;
    }
</style>

<script type="text/javascript">
    function myFunction(){
        var div = document.getElementById('test1');
        if(div.className.indexOf('test1') >= 0){
            div.className='test2';
        }else{
            div.className='test1';
        }
    }
</script>
</head>

<body>
    <div class="test1" id="test1" alt="" onclick="myFunction()">
    </div>
</body>
</html>

 这个方法解决了上一篇中无法读取识别十六进制颜色代码的问题。

posted @ 2016-09-26 17:41  木立里  阅读(8963)  评论(0编辑  收藏  举报