js的一些小练习

1.穿梭框

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .container {
            overflow: hidden;
            width: 550px;
            margin: 0 auto;
        }

        .container .left {
            float: left;
            margin: 0 30px;
        }

        .container select {
            width: 100%;
            height: 200px;
        }

        .container .mid {
            padding-top: 70px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="left">
            <h2>吴限的现任女友</h2>
            <select id="sel1" multiple>
                <option value="1">幂幂</option>
                <option value="2">花花</option>
                <option value="3">春春</option>
                <option value="4">盈盈</option>
                <option value="5">红红</option>
            </select>
        </div>
        <div class="left mid">
            <p>
                <button title="右移动选中的">&gt;&gt;</button>
            </p>
            <p>
                <button title="右移动全部">&gt;&gt;|</button>
            </p>
            <p>
                <button title="左移动选中的">&lt;&lt;</button>
            </p>
            <p>
                <button title="左移动全部">|&lt;&lt;</button>
            </p>
        </div>
        <div class="left">
            <h2>吴限的前女友</h2>
            <select id="sel2" multiple>
                <option value="6">坤坤</option>
            </select>
        </div>

    </div>
    <script>
        var but = document.getElementsByTagName('button');
        var sel1 = document.getElementById('sel1');
        var sel2 = document.getElementById('sel2');
        var op1 = sel1.getElementsByTagName('option');
        var op2 = sel2.getElementsByTagName('option');
        右移
        but[0].onclick = function(){
            for(i = 0; i<op1.length;i++){
                if(op1[i].selected){
                    sel2.appendChild(op1[i]);
                    i = -1;
                }
            }
        }

        // 全部右移
        but[1].onclick = function(){
            while(op1.length>0){
                sel2.appendChild(op1[0]);
            }
        }

        // 左移
        but[2].onclick = function(){
            for(i = 0; i<op2.length;i++){
                if(op2[i].selected){
                    sel1.appendChild(op2[i]);
                    i = -1;
                }
            }
        }

        // 全部左移
        but[3].onclick = function(){
            while(op2.length>0){
                sel1.appendChild(op2[0]);
            }
        }

        
     

    </script>
</body>

</html>

2.随机背景色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .container{
            width: 200px;
            height: 200px;
            border: 2px solid;
            cursor: pointer;
            background-color: aqua;
        }
    </style>
</head>
<body>
    <p>
        点击div,设置div的背景色为随机颜色
    </p>
    <div class="container">
    
    </div>
    <script>
       
        var div = document.getElementsByClassName('container')[0];
        div.onclick = function(){
        var a = Math.ceil(Math.random()*255);
        var b = Math.ceil(Math.random()*255);
        var c = Math.ceil(Math.random()*255);
        div.style.background = `rgb(${a},${b},${c})`;
        }
        
    </script>
</body>
</html>

今天的练习到此结束,谢谢大家

上一篇:nvidia Compute Capability(GPU)


下一篇:springmvc静态资源处理(mvc:default-servlet-handler)